使用ASP.NET Core Web API进行Facebook JWT身份验证

问题描述 投票:0回答:1

我正在使用ASP.NET Core 2.0 Web API来创建具有身份验证的应用程序。我想使用“登录/密码”组合并使用Facebook登录。我正在使用JWT令牌进行授权。来自Startup.cs我称之为扩展方法RegisterAuth

public static void RegisterAuth(this IServiceCollection services, AuthSettings authSettings)
{
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.SaveToken = true;
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = false,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = false,
                ValidIssuer = authSettings.Issuer,
                ValidAudience = authSettings.Audience,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authSettings.SecretKey))
            };
        })
        .AddFacebook(facebookOptions =>
        {
            facebookOptions.AppId = authSettings.Facebook.AppId;
            facebookOptions.AppSecret = authSettings.Facebook.AppSecret;
            facebookOptions.SignInScheme = "Bearer";
        });
}

在我的控制器中我有2种方法。 Login用于“登录/密码”组合,它工作并返回我jwt令牌。和SignIn为facebook无效。

[Route("SignIn/{provider}")]
public IActionResult SignIn(string provider)
{
    return Challenge(new AuthenticationProperties(), provider);
}

SignIn重定向到facebook页面,登录后会抛出异常。

InvalidOperationException:没有配置IAuthenticationSignInHandler来处理该方案的登录:Bearer

所以请帮我修复Facebook Auth。谢谢!

c# jwt asp.net-core-webapi facebook-authentication bearer-token
1个回答
0
投票

只需要更改Facebook SignInScheme并添加cookie。

1)将"Bearer";更改为CookieAuthenticationDefaults.AuthenticationScheme;并添加cookie。

2)在AddAuthentication添加之后

.AddCookie()
.AddFacebook(facebookOptions =>
{
    facebookOptions.AppId = authSettings.Facebook.AppId;
    facebookOptions.AppSecret = authSettings.Facebook.AppSecret;
    facebookOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
© www.soinside.com 2019 - 2024. All rights reserved.