Существует способ обойти это, вы можете создать заявку и получить этот способ:
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; }
}
Затем создайте метод расширения, чтобы получить свой 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;
}
}
}
А теперьВы можете легко вызвать этот метод, чтобы получить свой DepartmentId:
var departmentId= User.Identity.GetDepartmentId();