【问题标题】:Facebook JWT authentication using ASP.NET Core Web API使用 ASP.NET Core Web API 的 Facebook JWT 身份验证
【发布时间】:2019-09-03 16:51:40
【问题描述】:

我正在使用 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】:

    只需更改 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;
    });
    

    【讨论】:

    • 嗨@gor-asatryan,我不太了解所有身份验证方案,但我认为在这种情况下,您不再使用 JWT 方法,而是使用 Cookie 方法,不是吗?
    • 是的。使用 facebook cookie 身份验证和 jwt 登录+密码组合
    • 您如何使用 FB 处理 Token/Cookie 过期用例?还是只有在用户退出时才会改变?还是一生一世?
    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 2017-07-30
    • 2017-03-09
    • 1970-01-01
    • 2016-12-22
    相关资源
    最近更新 更多