【问题标题】:How to get user profile details in azure ad b2c如何在 azure ad b2c 中获取用户个人资料详细信息
【发布时间】:2017-10-07 08:45:32
【问题描述】:

我正在为我的 MVC Web 应用程序使用 Azure AD 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");
    } 
}

【问题讨论】:

    标签: asp.net-mvc azure-ad-b2c


    【解决方案1】:

    您需要在 B2C 政策中添加声明。

    选择策略 -> 编辑 -> 应用声明 -> 选择您想要的 -> 保存。

    当用户登录时,这些将被添加到他们的令牌中。然后,您可以在他们登录后在您的代码中枚举它们。:

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

    【讨论】:

      【解决方案2】:

      你有两个选择:

      选项 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);
          }
      
      1. 创建您的 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("/");
          }
      

      选项 2 - 实现您自己的编辑个人资料屏幕和体验 我不会详细介绍此选项,因为这很长,但在高层次上,您需要:

      1. 实现您自己的屏幕
      2. 实现您自己的受 Azure AD B2C 保护的 API(意味着它需要 Azure AD B2C 访问令牌)并拥有此 API use Client Credentials to update the user in question

      【讨论】:

        猜你喜欢
        • 2011-01-13
        • 2013-07-02
        • 2016-06-08
        • 2023-01-12
        • 2018-06-10
        • 2016-04-10
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多