2017-02-12 65 views
3

我已配置身份提供者以使用帶有用戶名而不是密碼的本地帳戶。Azure AD B2C - 使用「用戶名」不提供用戶名聲明的本地IDP

我創建了一個顯示名稱,用戶名和電子郵件的用戶。

我可以選擇「顯示名稱」和「電子郵件地址」所有政策申請要求,但「用戶名」是不是一種選擇。我還確認,用戶名稱聲明未提供給我的應用程序。

如何配置Azure AD B2C以使其提供用戶名聲明?

IDP Config

enter image description here

enter image description here

回答

2

不幸的是,用戶名還沒有提供可供選擇的要求繼續傳承令牌。你應該投票支持這個要求在Azure的AD B2C UserVoice的論壇,以幫助其優先級:因爲這個時間是通過圖形自行取回Include username in JWT claims

你唯一的選擇。

下面是Net代碼一個快速&骯髒的片段,您可以使用此:

private async Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) 
    { 
     try 
     { 
      var userObjectId = notification.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value; 

      // You'll need to register a separate app for this. 
      // This app will need APPLICATION (not Delegated) Directory.Read permissions 
      // Check out this link for more info: 
      // https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet 
      var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(string.Format(graphAuthority, tenant)); 
      var t = await authContext.AcquireTokenAsync(graphResource, new ClientCredential(graphClientId, graphClientSecret)); 

      string result; 
      using (var client = new HttpClient()) 
      { 
       client.DefaultRequestHeaders.Add("Authorization", "Bearer " + t.AccessToken); 

       var url = graphResource + tenant + "https://stackoverflow.com/users/" + userObjectId + "/?api-version=1.6"; 
       result = await client.GetStringAsync(url); 
      } 

      var jsonResult = JObject.Parse(result); 
      var username = jsonResult["signInNames"].FirstOrDefault(j => j["type"].ToString() == "userName")?["value"]?.ToString(); 
      notification.AuthenticationTicket.Identity.AddClaim(new Claim("username", username)); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
    } 

你會引用此方法,當你設置你的OpenIdConnectAuthenticationOptions像這樣:

new OpenIdConnectAuthenticationOptions 
     { 
      // (...) 
      Notifications = new OpenIdConnectAuthenticationNotifications 
      { 
       AuthenticationFailed = OnAuthenticationFailed, 
       SecurityTokenValidated = OnSecurityTokenValidated, 
      }, 
      // (...) 
     }; 
+0

以上看起來是一個很好的建議。這看起來很明顯,但對於那些不熟悉Azure B2C的人,不要忘記,爲了讓Azure B2C將ObjectId作爲標記中的聲明返回(http://schemas.microsoft.com/identity/claims/objectidentifier) ,您需要確保您在上面顯示的屏幕中檢查了用戶對象ID,以便您可以選擇要將哪些屬性包含在聲明中。 –

+0

我已經簽署了這個請求,因爲我覺得很奇怪,B2C在那裏驗證一個用戶,但是並沒有告訴我們哪個用戶...... – radders

相關問題