11

I have created a web application in .NET Core 2.0 where I would like to use a PrincipalContext from namespace System.DirectoryServices.AccountManagement.

I want to validate user agains Active Directory like this:

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;
            }
        }

How can I use the PrincipalContext (System.DirectoryServices.AccountManagement) in .NET Core 2.0?

4

1 回答 1

14

可以获得System.DirectoryServices.AccountManagementfor的预览版.NET Core 2.0

myget。可以通过这个feed通过 Nuget 包获得。关于这一点的扩展讨论在这里

更新: 最新的工作预览在这里

于 2017-08-24T19:43:35.323 回答