加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用OpenIdDictServer丰富的Twitter数字/ Google Auth

发布时间:2020-12-15 23:36:46 所属栏目:百科 来源:网络整理
导读:我们的应用需要通过手机号码或Google登录.我们计划通过Twitter数字进行手机号码验证. 我理解的注册和认证流程如下: 移动应用程序使用Twitter数字或Google登录进行丰富的身份验证(用户可以使用丰富的身份验证而不是打开Web浏览器选项卡,从而获得更好的用户体
我们的应用需要通过手机号码或Google登录.我们计划通过Twitter数字进行手机号码验证.

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

>移动应用程序使用Twitter数字或Google登录进行丰富的身份验证(用户可以使用丰富的身份验证而不是打开Web浏览器选项卡,从而获得更好的用户体验). Twitter Digits / Google Sign In返回身份令牌.
>移动应用程序将AuthServer调用到SignIn并显示身份令牌.
>身份服务器使用数字服务或Google身份验证服务验证呈现的身份令牌.
>一旦验证,AuthServer将尝试查找用户,如果不存在,它将创建一个.
> AuthServer将Access Token返回到移动应用程序.

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

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

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

解决方法

This works very well,but I am wondering what is the cleanest way to support what I am trying to achieve.

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

[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作为断言参数发送,它应该可以正常工作.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读