代码之家  ›  专栏  ›  技术社区  ›  Jesse Hallam

为什么OAuth在使用ASP.NET标识核心时失败?

  •  1
  • Jesse Hallam  · 技术社区  · 7 年前

    我有一个ASP.NET Core 2.x项目,配置如下:

    services
      .AddAuthentication(options => options.DefaultScheme = CookieAuthenticaitonDefaults.AuthenticationScheme)
      .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
      .AddFacebook(ConfigureFacebook);
    

    不出所料,当我从我的某个行为中调用时:

    return Challenge(new AuthenticationProperties { RedirectUri = "/test" }, "Facebook");

    HttpContext.User.Identity 包含相关详细信息:

    • User.Identity.Name
    • User.Identity.AuthenticationType -弦 "Facebook" .
    • User.Identity.IsAuthenticated - true

    这一切都很好,正如预期的那样。但是,如果我在应用程序配置中添加以下内容

    services.AddIdentity<MyUserType, MyRoleType>()
      .AddEntityFrameworkStores<MyDbContext>();
    

    突然,OAuth流结束了 User.Identity 匿名,没有任何改变。如果我们深入到identityservicecolectionextensions.cs,我们会发现:

    options.DefaultAuthenticateScheme选项= IdentityConstants.ApplicationScheme;options.DefaultChallengeScheme= IdentityConstants.ApplicationScheme;options.DefaultSignInScheme= IdentityConstants.ExternalScheme;

    除此之外。。。

    0 回复  |  直到 7 年前
        1
  •  2
  •   Edward    7 年前

    要组合OAuth和Asp.Net核心标识,需要配置 facebookOptions.SignInScheme 具有 CookieAuthenticationDefaults.AuthenticationScheme .

    services
        .AddAuthentication(options => options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddFacebook(facebookOptions =>
        {
            facebookOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            facebookOptions.AppId = "xx";
            facebookOptions.AppSecret = "xxx";
        });
    
        2
  •  1
  •   Jesse Hallam    7 年前

    在组合ASP.NET标识和OAuth时,需要考虑以下因素:

    配置服务:

    AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) 不再需要,因为标识添加了自己的cookie处理程序。

    将外部用户作为ClaimsPrincipal获取:

    如果要将外部用户填充到 HttpContext.User ,请执行以下操作:

    .AddFacebook(options => {
        options.SignInScheme = IdentityConstants.ApplicationScheme;
    })
    

    RedirectUri 在你的挑战中 AuthenticationProperties ,您的 将被填充。

    ExternalLoginInfo

    如果您需要了解有关用户的信息,例如:

    1. 他们是从哪家供应商来的?
    2. 他们在提供商上的唯一密钥是什么?

    您的服务配置应如下所示:

    services.AddAuthentication()
        .AddFacebook(options =>
        {
            options.AppId = "";
            options.AppSecret = "";
        });
    
    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<MyDbContext>();
    

    SignInManager<TUser> 在:

    public DefaultController(SIgnInManager<IdentityUser> signInManager)
    

    在你的挑战行动中,使用 ConfigureExternalAuthenticationProperties

    public IActionResult LoginExternal() {
        var props = SignInManager.ConfigureExternalAuthenticationProperties("Facebook", "/");
        return Challenge(props, "Facebook");
    }
    

    在你的回报行动中,使用 GetExternalLoginInfoAsync

    public async Task<IActionResult> LoginCallback() {
        var loginInfo = await SignInManager.GetExternalLoginInfoAsync();
        // This object will tell you everything you need to know about the incoming user.
    }