丰富的Twitter数字/ Google Auth与OpenIdDictServer

Abh*_*era 3 c# asp.net-authorization asp.net-authentication openiddict

我们的应用需要通过手机号码或Google登录.我们计划通过Twitter数字进行手机号码验证.

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

  1. 移动应用程序使用Twitter数字或Google登录进行丰富的身份验证(用户可以使用丰富的身份验证而不是打开Web浏览器选项卡,从而获得更好的用户体验).Twitter Digits/Google Sign In返回身份令牌.

  2. 移动应用程序将AuthServer调用到SignIn并显示身份令牌.

  3. 身份服务器使用Digits服务或Google Auth服务验证所呈现的身份令牌.

  4. 一旦验证,AuthServer将尝试查找用户,如果不存在,它将创建一个.

  5. AuthServer将Access Token返回给移动应用程序.

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

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

这非常有效,但我想知道什么是最干净的方式来支持我想要实现的目标.

Kév*_*let 7

这非常有效,但我想知道什么是最干净的方式来支持我想要实现的目标.

我个人会使用自定义授权类型:

[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."
    });
}
Run Code Online (Sandbox Code Playgroud)

请注意,您还必须在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();
Run Code Online (Sandbox Code Playgroud)

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

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

在实现令牌验证例程时要特别小心,因为此步骤特别容易出错.验证所有内容非常重要,包括受众(否则,您的服务器将容易受到混乱的副攻击).