【问题标题】:Rich Twitter Digits/Google Auth with OpenIdDictServer丰富的 Twitter 数字/Google Auth 与 OpenIdDictServer
【发布时间】:2017-05-04 13:40:47
【问题描述】:

我们的应用需要通过手机号码或 Google 登录。我们正计划通过 Twitter Digits 进行手机号码身份验证。

据我了解的注册和认证流程如下:

  1. 移动应用程序使用 Twitter Digits 或 Google 登录进行丰富的身份验证(用户执行丰富的身份验证而不是打开 Web 浏览器选项卡会带来更好的用户体验)。 Twitter Digits / Google 登录返回身份令牌。

  2. 移动应用调用 AuthServer 进行登录并提供身份令牌。

  3. 身份服务器使用 Digits 服务或 Google Auth 服务验证提供的身份令牌。

  4. 验证后,AuthServer 会尝试查找用户,如果不存在则会创建一个。

  5. AuthServer 向移动应用返回访问令牌。

现在,我正在寻找实施步骤 #3-4 的选项。

目前,我所做的是修改令牌端点代码,将用户名作为电话号码或电子邮件地址,密码作为 Google/Twitter Digits ID 令牌发送。现在,由于身份验证服务器需要知道发送的密码实际上是一个需要通过第三方服务验证的令牌,因此我通过在 TokenHint 中发送服务名称“Digits”或“Google”来解决它。

这很好用,但我想知道支持我想要实现的目标的最简洁方式是什么。

【问题讨论】:

    标签: c# asp.net-authorization asp.net-authentication openiddict


    【解决方案1】:

    这很好用,但我想知道支持我想要实现的目标的最简洁方式是什么。

    我个人会选择自定义授权类型:

    [HttpPost("~/connect/token")]
    [Produces("application/json")]
    public IActionResult Exchange(OpenIdConnectRequest request)
    {
        if (request.GrantType == "urn:ietf:params:oauth:grant-type:google_identity_token")
        {
            // Reject the request if the "assertion" parameter is missing.
            if (string.IsNullOrEmpty(request.Assertion))
            {
                return BadRequest(new OpenIdConnectResponse
                {
                    Error = OpenIdConnectConstants.Errors.InvalidRequest,
                    ErrorDescription = "The mandatory 'assertion' parameter was missing."
                });
            }
    
            // Create a new ClaimsIdentity containing the claims that
            // will be used to create an id_token and/or an access token.
            var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
    
            // Manually validate the identity token issued by Google,
            // including the issuer, the signature and the audience.
            // Then, copy the claims you need to the "identity" instance.
    
            // Create a new authentication ticket holding the user identity.
            var ticket = new AuthenticationTicket(
                new ClaimsPrincipal(identity),
                new AuthenticationProperties(),
                OpenIdConnectServerDefaults.AuthenticationScheme);
    
            ticket.SetScopes(
                OpenIdConnectConstants.Scopes.OpenId,
                OpenIdConnectConstants.Scopes.OfflineAccess);
    
            return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
        }
    
        return BadRequest(new OpenIdConnectResponse
        {
            Error = OpenIdConnectConstants.Errors.UnsupportedGrantType,
            ErrorDescription = "The specified grant type is not supported."
        });
    }
    

    请注意,您还必须在 OpenIddict 选项中启用它:

    // Register the OpenIddict services.
    services.AddOpenIddict()
        // Register the Entity Framework stores.
        .AddEntityFrameworkCoreStores<ApplicationDbContext>()
    
        // Register the ASP.NET Core MVC binder used by OpenIddict.
        // Note: if you don't call this method, you won't be able to
        // bind OpenIdConnectRequest or OpenIdConnectResponse parameters.
        .AddMvcBinders()
    
        // Enable the token endpoint.
        .EnableTokenEndpoint("/connect/token")
    
        // Enable the refresh token flow and a custom grant type.
        .AllowRefreshTokenFlow()
        .AllowCustomFlow("urn:ietf:params:oauth:grant-type:google_identity_token")
    
        // During development, you can disable the HTTPS requirement.
        .DisableHttpsRequirement();
    

    发送令牌请求时,请确保使用正确的grant_type 并将您的id_token 作为assertion 参数发送,它应该可以工作。

    以下是使用 Facebook 访问令牌的示例:

    在实施令牌验证例程时要非常小心,因为这一步特别容易出错。验证所有内容非常重要,包括观众(否则,your server would be vulnerable to confused deputy attacks)。

    【讨论】:

    • 这太完美了!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 2011-03-25
    • 2017-09-06
    • 2011-07-27
    • 2011-02-11
    • 2011-10-30
    • 1970-01-01
    相关资源
    最近更新 更多