Reputation: 2585
In my MVC application I have created two subclasses of the basic ASP Identity ApplicationUser
class using TPT inheritance, and would like to add some Claims to the objects to allow me to easily display the properties from the sub classes in views.
I must be missing a simple trick / have a fundamental misunderstanding of the ASP Identity setup, but I can't see how to do this.
Adding Claims to the ApplicationUser
class would be straightforward, but the GenerateUserIdentityAsync
method where you do this can't be overridden in the sub classes to allow me to do it there.
Is there a way to achieve this simply (as everything else with this set up is working nicely), or do I have to set up my two ApplicationUser
subclasses to directly inherit from IdentityUser
, and set up two lots of config for them both in IdentityConfig.cs
?
The classes I'm talking about are as follows:
//The ApplicationUser 'base' class
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string ProfilePicture { get; set; }
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
//** can add claims without any problems here **
userIdentity.AddClaim(new Claim(ClaimTypes.Name, String.Format("{0} {1}", this.FirstName, this.LastName)));I
return userIdentity;
}
}
public class MyUserType1 : ApplicationUser
{
[DisplayName("Job Title")]
public string JobTitle { get; set; }
//** How do I add a claim for JobTitle here? **
}
public class MyUserType2 : ApplicationUser
{
[DisplayName("Customer Name")]
public string CustomerName { get; set; }
//** How do I add a claim for CustomerName here? **
}
Upvotes: 2
Views: 804
Reputation: 2933
You can make GenerateUserIdentityAsync a virtual method in ApplicationUser which would allow you override the implementation in your concrete types.
That's the cleanest option that I can see.
Upvotes: 3