2015-09-09 10 views
5

Ich versuche, Active Directory-Authentifizierung für meine ASP.NET MVC-Anwendung. Ich benutze System.DirectoryServices und finde während der Anmeldung einen Benutzer im UserManager. Wenn der Benutzer nicht gefunden wird, versuche ich, einen Benutzer in Active Directory zu finden, und wenn er erfolgreich ist, registrieren Sie den Benutzer in der asp.net mvc app mit UserManager.CreateAsync().LDAP-Authentifizierung mit Asp.NET-Identität

Aber in meiner Implementierung ignoriert Fälle, wenn Benutzer Passwort in Active Directory-Konto oder AD-Konto wurde gelöscht. Ich kann es manuell in meinem Code überprüfen, aber gibt es vielleicht andere Möglichkeiten in ASP.NET Identity, Authentifizierung durch Active Directory-Benutzerkonto zu implementieren?

Antwort

0

sehen, ob dies helfen kann u

protected bool ActiveDirectoryLogin(string Username, string Password, string Domain) 
{ 
    bool Success = false; 
    //System.DirectoryServices.DirectoryEntry Entry = 
    // new System.DirectoryServices.DirectoryEntry("LDAP://196.15.32.161:389/cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", "uid=" + Username + ",cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", Password, AuthenticationTypes.None); 

    System.DirectoryServices.DirectoryEntry Entry = 
     new System.DirectoryServices.DirectoryEntry("LDAP://ldapmaster.kfupm.edu.sa:389/cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", "uid=" + Username + ",cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", Password,AuthenticationTypes.None); 

    //System.DirectoryServices.DirectoryEntry Entry = 
    // new System.DirectoryServices.DirectoryEntry("LDAP://ldapmaster.kfupm.edu.sa:389/cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa", Username , Password, AuthenticationTypes.None); 

    System.DirectoryServices.DirectorySearcher Searcher = new System.DirectoryServices.DirectorySearcher(Entry); 
    //Entry.Username = "uid="+Username + ",cn=KFUPM-People,o=KFUPM,dc=kfupm,dc=edu,dc=sa"; 
    //Entry.Password = Password; 
    //Entry.AuthenticationType = AuthenticationTypes.None; 
    // Searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree; 

    try 
    { 

     Object nat = Entry.NativeObject; 
     Success = true; 
//   System.DirectoryServices.SearchResult Results =  Searcher.FindOne(); 
//   Success = (Results != null); 

    } 
    catch (Exception e) 
    { 
     Success = false; 
    } 

    return Success; 
}