首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >富推特数字/Google与OpenIdDictServer

富推特数字/Google与OpenIdDictServer
EN

Stack Overflow用户
提问于 2016-12-19 13:28:52
回答 1查看 1.1K关注 0票数 3

我们的应用程序需要通过手机号码或谷歌登录。我们计划使用Twitter数字进行移动号码认证。

据我所知,登记和认证的流程如下:

  1. 移动应用程序使用Twitter数字或Google登录进行富身份验证(用户使用rich而不是打开web浏览器选项卡)更好的用户体验。Twitter数字/谷歌登录返回身份标记。
  2. 移动应用程序将AuthServer调用到SignIn,并显示身份标记。
  3. 身份服务器使用数字服务或Google服务验证所呈现的身份令牌。
  4. 一旦验证,AuthServer将尝试找到用户,如果不存在,它将创建一个用户。
  5. AuthServer将访问令牌返回给移动应用程序。

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

目前,我所做的是修改令牌端点代码,接受用户名作为电话号码或电子邮件地址和密码发送为Google/Twitter数字ID令牌。现在,由于auth服务器需要知道发送的密码实际上是一个需要第三方服务验证的令牌,所以我在TokenHint中发送服务名“位”或“Google”。

这是很好的工作,但我想知道什么是最清洁的方式来支持我正在努力实现的目标。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-19 14:00:04

这是很好的工作,但我想知道什么是最清洁的方式来支持我正在努力实现的目标。

我个人会选择一种定制的赠与类型:

代码语言:javascript
复制
[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选项中启用它:

代码语言:javascript
复制
// 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访问令牌的示例:

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41223694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档