2016-06-28 27 views
2

我需要一種方法通過IdentityServer3驗證組織內的用戶(使用LDAP和Active Directory),並授予他們對資源的訪問權限。[C#,.NET]:通過IdentityServer3通過LDAP驗證用戶

IdentityServer3似乎是OpenID Connect協議的實現框架,適用於身份驗證和授權。

到目前爲止,我已經能夠驗證硬編碼的用戶並使用InMemory實現獲取JWT(JSON Web令牌)訪問令牌。

請參考下面這個例子:

https://rajdeep.io/2015/05/07/creating-a-single-sign-on-using-thinktecture-identity-server-part-1/ 

但是,這不會是在我的情況非常有用,我就必須對用戶進行驗證(存儲在Active Directory)在大型組織中,併發給他們令牌訪問資源。

以下是我已經按照此鏈接

http://stackoverflow.com/questions/31536420/thinktecture-identityserver-v3-with-windowsauth)完成:

  1. 創建實現IUserService的ActiveDirectoryUserService。

    namespace LDAPSSO 
    { 
    public class ActiveDirectoryUserService : IUserService 
    { 
    private const string DOMAIN = "company domain"; 
    
    public Task<AuthenticateResult> AuthenticateExternalAsync(ExternalIdentity externalUser, SignInMessage message) 
    { 
        return Task.FromResult<AuthenticateResult>(null); 
    } 
    
    public Task<AuthenticateResult> AuthenticateLocalAsync(string username, string password, SignInMessage message) 
    { 
        try 
        { 
         using (var pc = new PrincipalContext(ContextType.Domain, DOMAIN)) 
         { 
          if (pc.ValidateCredentials(username, password)) 
          { 
           using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username)) 
           { 
            if (user != null) 
            { 
             return Task.FromResult(new AuthenticateResult(subject: Guid.NewGuid().ToString(), name: username)); 
            } 
           } 
          } 
    
          // The user name or password is incorrect 
          return Task.FromResult<AuthenticateResult>(null); 
         } 
        } 
        catch 
        { 
         // Server error 
         return Task.FromResult<AuthenticateResult>(null); 
        } 
        } 
    } 
    } 
    

參考:https://gist.github.com/tjrobinson/0ad6c790e90d7a385eb1

  • 創建實現IClientStore
  • 一個MyClientStore創建實現IScopeStore
  • 註冊的MyScopeStore這到工廠如下:

    public class Factory 
    { 
    public static IdentityServerServiceFactory Configure() 
    { 
    
        var factory = new IdentityServerServiceFactory 
        { 
         UserService = new Registration<IUserService, ActiveDirectoryUserService>(), // Don't need, but mandatory for idsvr3 
         ClientStore = new Registration<IClientStore, MyClientStore>(), 
         ScopeStore = new Registration<IScopeStore, MyScopeStore>() 
        }; 
        return factory; 
    
    } 
    
  • 即使我有這個工作,是否需要通過身份服務器提供的登錄表單比較用戶輸入的憑據? (請參見下圖):

    Authorization Server Login

    我需要以提取IdentityServer3登錄頁面,用戶憑證和驗證對Active Directory辦?

    這是正確的做法嗎?請建議。

    輸入和建議表示讚賞。

    預先感謝您!

    +0

    我正試圖用IdentityServer3來做同樣的事情。如果你有一段時間,你能幫我一把嗎? – blgrnboy

    回答

    2

    我將實現Active Directory作爲external identity provider

    通過這種方式,您不必維護任何自定義代碼,並在AD鎖定的情況下繼續使用AD的LDAP功能。您可以通過在Identity Server中禁用本地登錄或使用acr_values中的idp值,或者使用per client basis中的idp值來使其成爲您的唯一身份提供者。

    +0

    謝謝,斯科特。通過將我的CustomUserService類註冊到工廠,我能夠連接到組織的Active Directory。 當調用AuthenticateLocalAsync方法時,它調用 AuthenticateResult(user.Subject,GetDisplayName(user)); 'code' 可以將user.Subject參數設置爲任何值,並將此Subject值與某些值進行比較? – Benn

    +0

    實現Active Directory作爲外部身份提供者是否允許用戶在IdentityServer3登錄頁面輸入他們的憑證(很像舊的基本身份驗證)?還是你指的是使用WS-Federation身份提供者(但是針對Active Directory),這需要客戶端使用Windows身份驗證(利用在域用戶工作站上完成的身份驗證)? –