2016-08-09 5 views
0

Ich arbeite an einer App, die dem Benutzer ermöglicht, mit AD anmelden, verwende ich im Wesentlichen den gleichen Code wie wir in unserem geschützten Intranet verwenden, aber dieses Mal ist der Server nach vorne (oder ausgesetzt) zum Internet) Mein Problem ist, wenn ich mich mit einem ungültigen Passwort anmelde, erkennt es dies offensichtlich, aber wenn ich Informationen von der LDAP-Verbindung wie Gruppennamen abrufe sagt es, dass der Server nicht existiert. unten ist mein Code und die Stack-Trace Fehler, diese Dinge sind härter, um herauszufinden, wenn sie entfaltet ...LDAP auf vorwärts gerichteten Domänen

Controller

public ActionResult Login(LoginModel model, string returnUrl) 
{ 
    string logon_user = model.UserName.ToString(); 
    string logon_password = model.Password.ToString(); 

    ConnHelper connhelper = new ConnHelper(); 
    string encryptedTicket = null; 
    String adPath = "LDAP://dc1.servername.local/DC=servername,DC=local"; //Path to the LDAP directory server 
    ADAuthorize adAuth = new ADAuthorize(adPath); 
    FormsAuthenticationTicket authTicket = null; 

    try 
    { 
     if (true == adAuth.IsAuthenticated("dc1.servername.local", logon_user, logon_password)) 
     { 
      string groups = adAuth.GetGroups(); 

      Account acc = new Account(); 
      acc.windows_id = logon_user; 
      acc.password = logon_password; 
      acc.igers_id = connhelper.GetiGersID(acc.windows_id); 
      acc.email_address = acc.windows_id.ToString() + "@domain.com"; 
      acc.region = connhelper.IsNull(connhelper.GetRegionManager(acc.igers_id)); 
      acc.home_store_region = connhelper.IsNull(connhelper.GetHomeStoreRegion(acc.igers_id)); 
      acc.store_group = connhelper.IsNull(connhelper.GetStoreGroup(acc.igers_id)); 
      acc.home_store = connhelper.IsNull(connhelper.GetStore(acc.igers_id)); 
      acc.arr = connhelper.GetStores(acc.igers_id); 
      //acc.home_store_phone = misc.IsNull(misc.GetHomeStorePhoneNumber("hzs"), ""); 
      acc.home_store_phone = connhelper.IsNull(connhelper.GetHomeStorePhoneNumber(acc.igers_id), ""); 
      acc.full_name = connhelper.IsNull(connhelper.GetFullName(acc.igers_id), ""); 
      //ErrorLabel.Text += "windows=" + misc.GetStore(acc.igers_id); 

      //ErrorLabel.Text += "windows=" + acc.igers_id.ToString(); 

      //Add information to the session 
      Session.Add("roles", groups); 
      Session.Add("Account", acc); 

      // Create the authentication ticket 
      authTicket = 
      new FormsAuthenticationTicket(1, // version 
       acc.windows_id, 
       DateTime.Now, 
       DateTime.Now.AddMinutes(60), 
       false, groups); 
      // Now encrypt the ticket. 
      encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
      // Create a cookie and add the encrypted ticket to the cookie as data. 
      HttpCookie authCookie = 
       new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 
      // Add the cookie to the outgoing cookies collection. 
      Response.Cookies.Add(authCookie); 

      if (FormsAuthentication.GetRedirectUrl(acc.windows_id, false).EndsWith("Logout.aspx")) 
      { 
       return RedirectToAction("Login", "Account"); 
      } 

      // 
      // Validate code this does the redirect to where you want the logged in person to go to. 
      // 
      if (Url.IsLocalUrl(returnUrl)) 
      { 
       return Redirect(returnUrl); 
      } 
      else 
      { 
       return RedirectToAction("Index", "Home"); 
      } 
     } 
     else 
     { 
      ModelState.AddModelError("","Authentication failed, check username and password."); 
      return View(model); 
     } 
    } 
    catch (Exception ex) 
    { 
     ModelState.AddModelError("", "Error authenticating. " + ex.Message); 
     return View(model); 
    } 
    // return View(model); 
} 

Die beiden Teile des Modells, das ich das Problem herein glauben und die instantiate existiert die Klasse: (. dies wird eine ungültige Benutzername/Passmeldung zurück, damit ich weiß, seine Verbindungs)

class ADAuthorize 
{ 
    private string _path; 
    private string _filterAttribute; 

    public ADAuthorize(string path) 
    { 
     _path = path; 
    } 

erhalten Benutzer

public bool IsAuthenticated(string domain, string username, string pwd) 
{ 
    string domainAndUsername = domain + @"\" + username; 
    DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); 

    try 
    { 
     // Bind to the native AdsObject to force authentication. 
     Object obj = entry.NativeObject; 
     DirectorySearcher search = new DirectorySearcher(entry); 
     search.Filter = "(SAMAccountName=" + username + ")"; 
     search.PropertiesToLoad.Add("SAMAccountName"); 
     //search.PropertiesToLoad.Add("cn"); 
     SearchResult result = search.FindOne(); 
     if (null == result) 
     { 
      return false; 
     } 

     // Update the new path to the user in the directory 
     _path = result.Path; 
     _filterAttribute = (String)result.Properties["SAMAccountName"][0]; 
     //_filterAttribute = (String)result.Properties["cn"][0]; 
    } 
    catch (Exception ex) 
    { 
     throw new Exception(ex.Message); 
    } 
    return true; 
} 

Dies ist, wo es den Fehler wirft:

//Gets the Group 
public string GetGroups() 
{ 
    DirectorySearcher search = new DirectorySearcher(_path); 
    search.Filter = "(SAMAccountName=" + _filterAttribute + ")"; 
    //search.Filter = "(cn=" + _filterAttribute + ")"; 
    search.PropertiesToLoad.Add("memberOf"); 
    StringBuilder groupNames = new StringBuilder(); 

    try 
    { 
     SearchResult result = search.FindOne(); 
     int propertyCount = result.Properties["memberOf"].Count; 
     String dn; 
     int equalsIndex, commaIndex; 

     for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++) 
     { 
      dn = (String)result.Properties["memberOf"][propertyCounter]; 

      equalsIndex = dn.IndexOf("=", 1); 
      commaIndex = dn.IndexOf(",", 1); 
      if (-1 == equalsIndex) 
      { 
       return null; 
      } 

      groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)); 
      groupNames.Append("|"); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw new Exception("Error obtaining group names. this is where the error is thrown " + ex.Message + ex.StackTrace.ToString()); 
    } 
    return groupNames.ToString(); 
} 

Mein Fehler und Stack-Trace liest:

die angegebene Domäne ist nicht kontaktiert werden konnte nicht existiert oder nicht.

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 
at System.DirectoryServices.DirectoryEntry.Bind() 
at System.DirectoryServices.DirectoryEntry.get_AdsObject() 
at System.DirectoryServices.PropertyValueCollection.PopulateList() 
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) 
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName) 
at System.DirectoryServices.DirectorySearcher.get_SearchRoot() 
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) 
at System.DirectoryServices.DirectorySearcher.FindOne() 
at BoxCheckInApp.Controllers.ADAuthorize.GetGroups() 

Wieder funktioniert dies auf eine Flucht fein von der IDE, ich glaube, mein Problem ist der Weg, aber ich weiß nicht genug über AD zu wissen, was ich nicht weiß.

Antwort

0

nach einigem Graben erkannte ich, dass mein Problem mit dem Verzeichnissuchenden zusammenhing.

Ich habe die folgenden Zeilen

var searchRoot = new DirectoryEntry(_path, _user, _pass); 
DirectorySearcher search = new DirectorySearcher(searchRoot); 

und machte _USER und _pass private Variablen von ADAuthorize, die hat den Benutzername und Passwort entspricht, jetzt ist es zu verbinden.