There is a way around this, you can create a claim and get this way:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
// Add custom user claims here => this.DepartmentId is a value stored in database against the user
userIdentity.AddClaim(new Claim("DepartmentId", this.DepartmentId.ToString()));
return userIdentity;
}
// Your Extended Properties
public int? DepartmentId { get; set; }
}
Then create an extension method to get your departmentId:
namespace App.Extensions
{
public static class IdentityExtensions
{
public static string GetDepartmentId(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("DepartmentId");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
}
}
And now you can easily call this method to get your DepartmentId:
var departmentId= User.Identity.GetDepartmentId();