2011-02-28 122 views
3

我們有一個網站,可以作爲員工和互聯網的外部人員使用的Intranet。我們所有的員工賬號都在Active Directory中。因此,當內部員工瀏覽URL(例如http://app.abc.com)時,他們應該使用他們的AD帳戶自動登錄。用c#ASP.Net中的Active Directory帳戶自動登錄網站

但是,對於外部用戶,他們必須使用他們的用戶名和密碼。只需查找數據庫並進行身份驗證,我就可以輕鬆完成這部分任務。

我想知道如何自動登錄AD用戶進入網站。

回答

0
public bool Authenticate(string userName, string passwd) 
     { 
      //Domain . 
      string domain = "YOUR_DOMAIN_NAME"; 
      string domainAndUsername = domain + @"\" + userName; 
      DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, passwd); 

      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("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["cn"][0]; 
      } 
      catch (Exception ex) 
      { 

       PageLogger.AddToLogError("AUTH_ERROR", ex); 
       return false; 

      } 

      return true; 
     } 

     private string GetGroups() 
     { 
      DirectorySearcher search = new DirectorySearcher(_path); 
      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. " + ex.Message); 
      } 
      return groupNames.ToString(); 
     }