我已經在.NET Core 2.0中創建了一個Web應用程序,我希望使用名稱空間System.DirectoryServices.AccountManagement
中的PrincipalContext
。如何在.NET Core 2.0中使用PrincipalContext
我想驗證用戶agains的Active Directory這樣的:
private static ClaimsIdentity ValidateUser(string userName, string password)
{
var domain = GetDomainByLogin(userName);
using (var pc = new PrincipalContext(ContextType.Domain, domain, null, ContextOptions.Negotiate))
{
if (!pc.ValidateCredentials(userName, password)) return null;
var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName);
if (user == null)
{
throw new Exception(UserNotFound);
}
var id = new ClaimsIdentity();
id.AddClaim(new Claim(JwtClaimTypes.Subject, userName));
id.AddClaim(new Claim(JwtClaimTypes.Name, userName));
var groups = user.GetGroups();
var roles = groups.Select(x => new Claim(JwtClaimTypes.Role, x.Name));
id.AddClaims(roles);
return id;
}
}
我怎樣才能在PrincipalContext
(System.DirectoryServices.AccountManagement
)在.NET Core 2.0
使用?