2017-05-09 91 views
0

我正在爲我的MVC Web應用程序使用Azure AD B2C身份驗證。我已經開發了項目的登錄部分。現在我想在用戶登錄到Web應用程序時獲取用戶的詳細信息。我見過一些解釋如何編輯用戶詳細信息的文章。但是我找不到任何與用戶配置文件數據相關的內容。請幫忙。如何在azure廣告中獲取用戶個人資料詳細信息b2c

這是我的登錄操作。

public ActionResult SignIn() 
{ 
    if (!Request.IsAuthenticated) 
    { 
     var authenticationManager = HttpContext.GetOwinContext().Authentication; 
     authenticationManager.Challenge(new AuthenticationProperties() { RedirectUri = "/" }, Startup.SignInPolicyId); 
     return Content(""); 
    } 
    else 
    { 
     return Redirect("~/Home/Login"); 
    } 
} 

回答

0

你有兩個選擇:

選項1,首選 - 使用Azure的AD B2C的編輯個人資料功能

  1. Create an Edit Profile Policy

  2. 添加logic on the RedirectToIdentityProvider handler覆蓋策略時呼叫Azure AD B2C

/* 
    * On each call to Azure AD B2C, check if a policy (e.g. the profile edit or password reset policy) has been specified in the OWIN context. 
    * If so, use that policy when making the call. Also, don't request a code (since it won't be needed). 
    */ 
    private Task OnRedirectToIdentityProvider(RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) 
    { 
     var policy = notification.OwinContext.Get<string>("Policy"); 

     if (!string.IsNullOrEmpty(policy) && !policy.Equals(DefaultPolicy)) 
     { 
      notification.ProtocolMessage.Scope = OpenIdConnectScopes.OpenId; 
      notification.ProtocolMessage.ResponseType = OpenIdConnectResponseTypes.IdToken; 
      notification.ProtocolMessage.IssuerAddress = notification.ProtocolMessage.IssuerAddress.Replace(DefaultPolicy, policy); 
     } 

     return Task.FromResult(0); 
    } 
  • 創建你EditProfile controller action確保其指示EditProfilePolicy應使用:
  • public void EditProfile() 
        { 
         if (Request.IsAuthenticated) 
         { 
          // Let the middleware know you are trying to use the edit profile policy (see OnRedirectToIdentityProvider in Startup.Auth.cs) 
          HttpContext.GetOwinContext().Set("Policy", Startup.EditProfilePolicyId); 
    
          // Set the page to redirect to after editing the profile 
          var authenticationProperties = new AuthenticationProperties { RedirectUri = "/" };    HttpContext.GetOwinContext().Authentication.Challenge(authenticationProperties); 
          return; 
         } 
         Response.Redirect("/"); 
        } 
    

    OPTION 2 - 實施你的自己編輯個人資料屏幕和體驗 我不會詳細介紹此選項離子因爲這是相當漫長的,但在較高的水平,你將需要:

    1. 實現你自己的屏幕
    2. 實現自己的API通過Azure的AD B2C擔保(這意味着它需要和Azure的AD B2C訪問令牌)並有此API use Client Credentials to update the user in question
    0

    在B2C政策中,您需要添加版權聲明。

    選擇策略 - >編輯 - >應用程序聲明 - >選擇你想要的 - >保存。

    當使用登錄時,這些將被添加到他們的令牌。您可以在登錄後再枚舉它們。:

    var claimsIdentity = (System.Security.Claims.ClaimsIdentity)User.Identity; 
    foreach (var claim in claimsIdentity.Claims) 
    { 
        // do stuff with claim.Type & claim.Value 
    } 
    
    相關問題