简体   繁体   中英

Access custom properties on ApplicationUser class in EF Core 2.0.1

I am working on a .NET Core application coupled with EF Core 2.0.1 (MySQL via Pomelo).

I have the following in my ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
  public string DisplayUsername { get; set; }
}

What is the correct way of accessing that DisplayUsername property in my BaseController.cs ? In .NET Framework, I used to do this in my BaseController.cs :

public class BaseController : Controller
{
  public BaseController()
  {
    var prinicpal = (ClaimsPrincipal)Thread.CurrentPrincipal;
    var displayUsername = prinicpal.Claims.Where(c => c.Type == ClaimTypes.GivenName).Select(c => c.Value).SingleOrDefault();
    ViewBag.DisplayUsername = displayUsername;
  }
}

But that doesn't work anymore, because we can no longer do Thread.CurrentPrinciple . What is the right way in the latest stable release of .NET Core?

There is now a User property in controller class (defined in ControllerBase class):

 // Gets the System.Security.Claims.ClaimsPrincipal for user 
 // associated with the executing action.
 public ClaimsPrincipal User { get; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM