Viktor
Viktor

Reputation: 507

User.Identity.Name is null when integrating ASP.NET Identity with OWIN auth to Azure AD B2C

I am extending an existing MVC web application which uses ASP.NET Identity for authentication. It uses System.Web.HttpContext.Current.User.Identity.Name for getting the username of the currently logged on user.

When I switch to OWIN authentication using the sample code explained here the Name property is null, causing the existing application to throw an error. Is it possible to change the value of IIdentity.Name without invalidating the identity?

Upvotes: 1

Views: 3403

Answers (1)

Chris Padgett
Chris Padgett

Reputation: 14634

As described at the bottom of the referenced tutorial, you should access the user claims via the ClaimsPrincipal.Current object, such as:

// 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();
}

Upvotes: 1

Related Questions