How to use PrincipalContext in .NET Core 2.0

て烟熏妆下的殇ゞ 提交于 2020-01-14 07:36:10

问题


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?


回答1:


It is possible get the preview version of System.DirectoryServices.AccountManagement for .NET Core 2.0.

From myget. It is possible get via Nuget package via this feed. The extended discussion about that is here.

Update: Latest working preview is here.



来源:https://stackoverflow.com/questions/45816418/how-to-use-principalcontext-in-net-core-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!