|
namespace App.Web.Extensions |
|
{ |
|
public static class DirectoryExtensions |
|
{ |
|
public static Task<UserPrincipal> GetUserPrincipal(this IIdentity identity) |
|
{ |
|
return Task.Run(() => |
|
{ |
|
try |
|
{ |
|
WindowsIdentity windowsIdentity = identity as WindowsIdentity; |
|
PrincipalContext context = new PrincipalContext(ContextType.Domain); |
|
UserPrincipal principal = new UserPrincipal(context); |
|
|
|
if (context != null) |
|
{ |
|
principal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, windowsIdentity.Name); |
|
} |
|
|
|
return principal; |
|
} |
|
catch (Exception ex) |
|
{ |
|
throw new Exception(ex.GetExceptionMessageChain()); |
|
} |
|
}); |
|
} |
|
|
|
public static Task<UserPrincipal> GetUserPrincipal(this Guid guid) |
|
{ |
|
return Task.Run(() => |
|
{ |
|
try |
|
{ |
|
PrincipalContext context = new PrincipalContext(ContextType.Domain); |
|
UserPrincipal principal = new UserPrincipal(context); |
|
|
|
if (context != null) |
|
{ |
|
principal = UserPrincipal.FindByIdentity(context, IdentityType.Guid, guid.ToString()); |
|
} |
|
|
|
return principal; |
|
} |
|
catch (Exception ex) |
|
{ |
|
throw new Exception(ex.GetExceptionMessageChain()); |
|
} |
|
}); |
|
} |
|
|
|
public static Task<List<UserPrincipalModel>> FindDomainUser(this string username) |
|
{ |
|
return Task.Run(() => |
|
{ |
|
PrincipalContext context = new PrincipalContext(ContextType.Domain); |
|
UserPrincipal principal = new UserPrincipal(context); |
|
principal.UserPrincipalName = $"*{username}*"; |
|
principal.DisplayName = "*,*"; |
|
principal.Enabled = true; |
|
PrincipalSearcher searcher = new PrincipalSearcher(principal); |
|
|
|
var users = searcher.FindAll() |
|
.AsQueryable() |
|
.Cast<UserPrincipal>() |
|
.FilterUsers() |
|
.SelectUsers() |
|
.OrderBy(x => x.displayName) |
|
.ToList(); |
|
|
|
return users; |
|
}); |
|
} |
|
|
|
public static Task<List<UserPrincipalModel>> GetDomainUsers() |
|
{ |
|
return Task.Run(() => |
|
{ |
|
PrincipalContext context = new PrincipalContext(ContextType.Domain); |
|
UserPrincipal principal = new UserPrincipal(context); |
|
principal.UserPrincipalName = "*@*"; |
|
principal.DisplayName = "*,*"; |
|
principal.Enabled = true; |
|
PrincipalSearcher searcher = new PrincipalSearcher(principal); |
|
|
|
var users = searcher.FindAll() |
|
.AsQueryable() |
|
.Cast<UserPrincipal>() |
|
.FilterUsers() |
|
.SelectUsers() |
|
.ToList(); |
|
|
|
return users; |
|
}); |
|
} |
|
|
|
public static IQueryable<UserPrincipal> FilterUsers(this IQueryable<UserPrincipal> users) |
|
{ |
|
return users.Where(x => x.Guid.HasValue // and / or any additional filters); |
|
} |
|
|
|
public static IQueryable<UserPrincipalModel> SelectUsers(this IQueryable<UserPrincipal> users) |
|
{ |
|
return users.Select(x => new UserPrincipalModel |
|
{ |
|
guid = x.Guid.Value, |
|
displayName = x.DisplayName, |
|
samAccountName = x.SamAccountName, |
|
userPrincipalName = x.UserPrincipalName, |
|
distinguishedName = x.DistinguishedName |
|
}); |
|
} |
|
} |
|
} |