This will be the base class for all you users
public abstract class User : IdentityUser
{
public abstract string Area { get; }
public bool IsActiveDirectoryUser { get; private set; }
protected User(string username, bool isActiveDirectoryUser = false)
: base(username)
{
IsActiveDirectoryUser = isActiveDirectoryUser;
}
protected User()
{ }
}
This is an example of user
public class AdminUser : User
{
public AdminUser(string username, bool isActiveDirectoryUser = false)
: base(username, isActiveDirectoryUser)
{ }
private AdminUser()
{ }
public override string Area
{
get { return UserAreas.Admin; }
}
}
This is the DBContext, with the mappings ignoring the user are property because It is hard coded
public class IdentityDataContext : IdentityDbContext<User>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().ToTable("AspNetUsers").Ignore(u => u.Area);
modelBuilder.Entity<AdminUser>().ToTable("AdminUsers");
}
}
This is your custom implementation of IUserManager. It works over the entity User, using the IdentityDataContext we already defined and our custom ClaimsFactory if nesesary as shown below
public class UserManager
{
private readonly UserManager<User> _identityManager;
public UserManager(ClaimsFactory claimsFactory, IdentityDataContext context, IdentityValidator identityValidator)
{
_identityManager = new UserManager<User>(new UserStore<User>(context))
{
ClaimsIdentityFactory = claimsFactory,
UserValidator = identityValidator
};
}
public void Register(User user, string password)
{
var result = _identityManager.Create(user, password);
if (!result.Succeeded)
throw new ApplicationException("User can not be created. " + result.Errors.FirstOrDefault());
}
public void Register(User user)
{
var result = _identityManager.Create(user);
if (!result.Succeeded)
throw new ApplicationException("User can not be created. " + result.Errors.FirstOrDefault());
}
public User Find(string userName, string password)
{
return _identityManager.Find(userName, password);
}
public ClaimsIdentity CreateIdentity(User user, string applicationCookie)
{
return _identityManager.CreateIdentity(user, applicationCookie);
}
public User FindByUserName(string userName)
{
return _identityManager.FindByName(userName);
}
public bool ChangePassword(string identifier, string oldPassword, string newPassword)
{
return _identityManager.ChangePassword(identifier, oldPassword, newPassword).Succeeded;
}
public bool ResetPassword(string userName, string password)
{
try
{
var user = FindByUserName(userName);
var result = _identityManager.RemovePassword(user.Id);
if (result != IdentityResult.Success)
return false;
result = _identityManager.AddPassword(user.Id, password);
return result == IdentityResult.Success;
}
catch (Exception)
{
return false;
}
}
public User FindById(string userId)
{
return _identityManager.FindById(userId);
}
public bool IsInRole(string userId, string role)
{
return _identityManager.IsInRole(userId, role);
}
public void AddToRole(string userId, string role)
{
_identityManager.AddToRole(userId, role);
}
}
If you like to have claims, this is the claims factory. It converts de user area into a claim, and finally a cookie in the browser.
public class ClaimsFactory : ClaimsIdentityFactory<User>
{
public async override Task<ClaimsIdentity> CreateAsync(UserManager<User> manager, User user, string authenticationType)
{
var identity = await base.CreateAsync(manager, user, authenticationType);
identity.AddClaim(new Claim(ClaimTypes.Area, user.Area, ClaimValueTypes.String));
return identity;
}
}
IdentityUser. And all the otherIdentity{something}then make sure yourUserManagerandUserStoreuse these new ones.ApplicationUseris just inherited fromIdentityUser