名前空間のPrincipalContext
を使用したい.NET Core 2.0でWebアプリケーションを作成しましたSystem.DirectoryServices.AccountManagement
。
次のように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
?