【问题标题】:User.Identity.Name is null when integrating ASP.NET Identity with OWIN auth to Azure AD B2C将 ASP.NET 身份与 OWIN 身份验证集成到 Azure AD B2C 时,User.Identity.Name 为空
【发布时间】:2017-11-27 14:47:15
【问题描述】:

我正在扩展一个使用 ASP.NET Identity 进行身份验证的现有 MVC Web 应用程序。它使用System.Web.HttpContext.Current.User.Identity.Name 获取当前登录用户的用户名。

当我使用here 解释的示例代码切换到 OWIN 身份验证时,Name 属性为空,导致现有应用程序抛出错误。是否可以在不使身份失效的情况下更改IIdentity.Name 的值?

【问题讨论】:

  • 不确定,但如果您在应用程序中返回 Display Name,声称它不会再是 null 了 :) 它不会像以前那样是 username

标签: asp.net asp.net-identity claims-based-identity azure-ad-b2c


【解决方案1】:

the referenced tutorial底部所述,您应该通过ClaimsPrincipal.Current对象访问用户声明,例如:

// Controllers\HomeController.cs

[Authorize]
public ActionResult Claims()
{
    Claim displayName = ClaimsPrincipal.Current.FindFirst(ClaimsPrincipal.Current.Identities.First().NameClaimType);
    ViewBag.DisplayName = displayName != null ? displayName.Value : string.Empty;
    return View();
}

【讨论】:

  • System.Security.Claims.ClaimsPrincipal.Current
猜你喜欢
  • 2019-06-25
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 2020-08-26
  • 2016-05-06
  • 1970-01-01
  • 2016-04-26
相关资源
最近更新 更多