如何在.NET Core 2.0中使用PrincipalContext

Jen*_*nan 8 active-directory principalcontext asp.net-core

我在.NET Core 2.0中创建了一个Web应用程序,我想使用PrincipalContextfrom命名空间 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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

我如何使用PrincipalContext(System.DirectoryServices.AccountManagement).NET Core 2.0

Jen*_*nan 11

有可能获得System.DirectoryServices.AccountManagementfor 的预览版本.NET Core 2.0.

来自myget - System.DirectoryServices.AccountManagement

可通过此Feed获取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