2017-08-22 112 views
0

我已經在.NET Core 2.0中創建了一個Web應用程序,我希望使用名稱空間System.DirectoryServices.AccountManagement中的PrincipalContext如何在.NET Core 2.0中使用PrincipalContext

我想驗證用戶agains的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; 
      } 
     } 

我怎樣才能在PrincipalContextSystem.DirectoryServices.AccountManagement)在.NET Core 2.0使用?

回答

1

對於.NET Core 2.0,可以獲得System.DirectoryServices.AccountManagement的預覽版本。

從myget - https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.DirectoryServices.AccountManagement

有可能通過這種飼料通過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