加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:34:30 所属栏目:asp.Net 来源:网络整理
导读:我在.NET Core 2.0中创建了一个Web应用程序,我想在命名空间System.DirectoryServices.AccountManagement中使用PrincipalContext. 我想再次验证用户的Active Directory,如下所示: private static ClaimsIdentity ValidateUser(string userName,string passwo
我在.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.0的System.DirectoryServices.AccountManagement的预览版本.

来自myget – https://dotnet.myget.org/feed/dotnet-core/package/nuget/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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读