While I'm not using ASP.NET Identity in production yet (perhaps in a few months) I do have a test project put together that authenticates against active directory for internal users. You will have to roll your own implementation to make this work though.
Here are some shortened samples to show how I am hitting active directory in different methods:
Roles
public async Task<IList<string>> GetRolesAsync(User user) {
List<string> roles = new List<string>();
//Active Directory Roles
if (user.Email.Contains("@mycompany")) {
var directory = new CompanyDirectory();
var adGroups = directory.GetGroupsByUser(user.Email);
if (adGroups != null && adGroups.Count > 0) {
roles.AddRange(adGroups);
}
}
//SQL Server Roles
var dbRoles = await _context.Users
.Where(u => u.UserName == user.UserName)
.SelectMany(u => u.Roles)
.Select(r => r.Name)
.ToListAsync();
roles.AddRange(dbRoles);
return roles;
}
Auth
public override async Task<User> FindAsync(string userName, string password) {
var identityUser = await base.FindByNameAsync(userName);
if (identityUser != null) {
if (userName.EndsWith("@mycompany.net")) {
var directory = new CompanyDirectory();
var isValidated = directory.ValidateUser(userName, password);
if (isValidated) {
return identityUser;
}
} else {
//SQL Server Auth
}
}
return null;
}
You don't need to extend the IdentityUser
class as that is actually the default class used by the Entity Framework implementation (even though I use SQL Server, my database schema is much different than the default ASP.NET Identity implementation so I use my own models). At the very least what you need to implement is IUser
(this is actually what the IdentityUser
implements). Here is how I am implementing it:
public partial class User : IUser<Guid> {
}
I have another partial class with the same name which contains all the properties and information used by the Entity Framework.