3

我們的應用程序需要通過手機號碼或谷歌登錄。我們計劃使用Twitter數字進行手機號碼身份驗證。豐富的Twitter數字/ Google身份驗證與OpenIdDictServer

註冊和認證的,因爲我理解的流程如下:

  1. 移動應用程序確實與Twitter位數或富驗證谷歌登陸(這是更好的用戶體驗,爲用戶做豐富的權威性,而不是打開一個Web瀏覽器標籤)。 Twitter數字/ Google登錄返回身份令牌。

  2. 移動應用程序調用AuthServer SignIn並顯示身份標記。

  3. 身份服務器驗證提供的帶有數字的身份令牌服務或Google身份驗證服務。

  4. 驗證完成後,AuthServer會嘗試找到用戶,如果不存在,它將創建一個。

  5. AuthServer將Access令牌返回給移動應用程序。

現在,我正在尋找實施步驟#3-4的選項。

目前,我所做的是修改以用戶名作爲電話號碼或電子郵件地址和密碼作爲Google/Twitter Digits ID令牌發送的令牌端點代碼。現在,由於auth服務器需要知道發送的密碼實際上是需要使用第三方服務驗證的令牌,所以我通過在TokenHint中發送服務名稱「Digits」或「Google」來解決此問題。

這很好,但我想知道什麼是最乾淨的方式來支持我想要實現的。

回答

5

這工作得很好,但我想知道什麼是最乾淨的方式來支持我想要實現的。

我會親自去與一個自定義的補助類型:

[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)。

+0

這是完美的!非常感謝。 –