如何在.NET Core 2.0中使用PrincipalContext
问题描述:
我已经在.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
使用?
答
对于.NET Core 2.0
,可以获得System.DirectoryServices.AccountManagement
的预览版本。
从myget - https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.DirectoryServices.AccountManagement
有可能通过这种饲料通过NuGet包得:https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
有关的广泛的讨论是在这里:https://github.com/dotnet/corefx/issues/2089
更新: 最新工作预览这里: https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.DirectoryServices.AccountManagement/4.5.0-preview1-25726-06