gpt4 book ai didi

asp.net-core - 如何在.NET Core 2.0中使用PrincipalContext

转载 作者:行者123 更新时间:2023-12-01 16:13:58 28 4
gpt4 key购买 nike

我在 .NET Core 2.0 中创建了一个 Web 应用程序,我想在其中使用命名空间 System.DirectoryServices.AccountManagement 中的 PrincipalContext

我想像这样再次验证用户的 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;
}
}

如何在 .NET Core 2.0 中使用 PrincipalContext (System.DirectoryServices.AccountManagement)?

最佳答案

可以获取 .NET Core 2.0System.DirectoryServices.AccountManagement 预览版。

来自myget 。可以通过此 feed 通过 Nuget 包获取。关于此问题的扩展讨论是 here .

更新:最新工作预览是 here .

关于asp.net-core - 如何在.NET Core 2.0中使用PrincipalContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45816418/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com