【问题标题】:How to use PrincipalContext in .NET Core 2.0如何在 .NET Core 2.0 中使用 PrincipalContext
【发布时间】:2017-08-22 11:29:20
【问题描述】:

我在 .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)?

【问题讨论】:

    标签: asp.net-core active-directory principalcontext


    【解决方案1】:

    对于.NET Core 2.0,可以获得System.DirectoryServices.AccountManagement的预览版。

    来自myget。可以通过这个feed 获得 Nuget 包。 对此的扩展讨论是here

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 2018-05-28
      • 2018-12-14
      • 2018-01-28
      • 2018-04-05
      • 1970-01-01
      • 2018-11-17
      相关资源
      最近更新 更多