Asp.net-Core
如何在 .NET Core 2.0 中使用 PrincipalContext
我在 .NET Core 2.0 中創建了一個 Web 應用程序,我想在其中使用
PrincipalContextfrom namespaceSystem.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?
可以獲得
System.DirectoryServices.AccountManagementfor的預覽版.NET Core 2.0。從myget。可以通過這個feed通過 Nuget 包獲得。關於這一點的擴展討論在這裡。
更新: 最新的工作預覽在這裡。