2017-09-21 16 views
0

私はLDAP関連のコーディングが初めてで、今日はLDAPに対するユーザー認証を確認するコードを開発するよう求められています。C#LDAPでユーザーを認証する方法

私がオンラインで見つけたチュートリアルはとてもシンプルですが、私たちの会社のディレクトリはとても複雑で、そのためのコードを書く方法がわかりません。ここでは、LDAPの情報です。私は名前を隠すために会社名を変更しました。ここで

uri = ldaps://ABC.ad.XYZ.com:636 
user_filter = memberOf=CN=TENXAIRFLOWPROD,OU=Security Groups,OU=Normal Users and Groups,OU=Account Management Services,OU=AD Master OU,DC=ABC,DC=ad,DC=XYZ,DC=com 
user_name_attr = sAMAccountName 
superuser_filter = memberOf=CN=TENXAIRFLOWPROD_ADM,OU=Security Groups,OU=Normal Users and Groups,OU=Account Management Services,OU=AD Master OU,DC=ABC,DC=ad,DC=XYZ,DC=com 
bind_user = SCGLOBAL\twiki 
bind_password_cmd = python /bns/tenx/airflow/ldap_password.py 
basedn = DC=ABC,DC=ad,DC=XYZ,DC=com 
search_scope = SUBTREE 

私が開発したコードですが、それは私にエラーを与える:

string username = "myUserName"; 
string domain = "ldaps://ABC.ad.XYZ.com:636"; 
string pwd = "myPasword";    
try 
{ 
    DirectoryEntry entry = new DirectoryEntry(domain, username, pwd); 
    //Bind to the native AdsObject to force authentication. 
    object obj = entry.NativeObject; 
    lblError.Text=("Login Successful"); 

    //search some info of this user if any 
    DirectorySearcher search = new DirectorySearcher(entry); 
    search.Filter = "(SAMAccountName=" + username + ")"; 
    SearchResult result = search.FindOne(); 
} 
catch (Exception ex) 
{ 
    lblError.Text=("Login failed: " + ex.ToString()); 
} 

は誰がplzは助けてもらえますか?

+0

Microsoft Active Directoryにアクセスしていますか? – Win

+0

管理人によると、私はADのグループに割り当てられました。しかし、どうすればそれにアクセスできるのかを確認することはできますか? – Navid

答えて

1

Comment: According to the admin , I have been assigned to the group in AD. But how can I make sure I can access it?

Active Directoryのようです。その場合は、PrincipalContextを使用してください。

public bool ValidateCredentials(string domain, string username, string password) 
{ 
    using (var context = new PrincipalContext(ContextType.Domain, domain)) 
    { 
     return context.ValidateCredentials(username, password); 
    } 
} 

public bool IsUserInAdGroup(string domain, string username, string adGroupName) 
{ 
    bool result = false; 
    using (var context = new PrincipalContext(ContextType.Domain, domain)) 
    { 
     var user = UserPrincipal.FindByIdentity(context, username); 
     if (user != null) 
     { 
      var group = GroupPrincipal.FindByIdentity(context, adGroupName); 
      if (group != null && user.IsMemberOf(group)) 
       result = true; 
     } 
    } 
    return result; 
} 

System.DirectoryServices.AccountManagementを必ず参照してください。

+0

これらすべてのuser_filter、user_name_attr、DC、OUなどをどこで使用するかはまだ分かっています。 – Navid

+0

通常、特定の要件を持っていない限り、PrincipalContextを使用してActive Directoryを呼び出す場合、手動で構築する必要はありません。 Microsoft以外のディレクトリサービスにアクセスしています。 – Win

関連する問題