2016-08-09 12 views
0

ADを使用してユーザーがログインできるアプリで作業していますが、保護されたイントラネットで使用しているコードと本質的に同じコードを使用していますが、私の問題は、私が無効なパスワードでログオンすると明らかにこれを認識しますが、グループ名などのLDAP接続から情報が得られるようになると、サーバーは存在しません。以下の私のコードとスタックトレースのエラーがある、これらのものは、展開...正面ドメインのLDAP

コントローラ

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); 
} 

私は、問題が存在する中で信じているモデルとインスタンス化の2つの部分を把握する困難ですクラスの:(。これは無効なユーザー名/パス・メッセージを返しますので、私はその接続が知っている)

class ADAuthorize 
{ 
    private string _path; 
    private string _filterAttribute; 

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

は、ユーザーが取得

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; 
} 

それがエラーをスローする場所です:

//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(); 
} 

私のエラーとスタックトレースが読み取ります

指定されたドメインが存在しないか、またはアクセスできません。

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() 

ここでも、これはIDEから実行時に正常に動作します、私は、私の問題はパスだと思う が、私は知らない何を知っているADについて十分に知りません。

答えて

0

私は自分の問題がディレクトリサーチャーに関連していたことを認識しました。

私は次の行に

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

を追加し、_userを行い、送信されたユーザ名とパスワードに対応ADAuthorizeのプライベート変数を_pass、今それが接続されます。

関連する問題