2011-05-18 88 views
0
public Object IsAuthenticated() 
{ 
    String domainAndUsername = strDomain + "\\" + strUser; 
    ***DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, strPass);*** 
    SearchResult result; 
    try 
    { 
     //Bind to the native AdsObject to force authentication.   

     DirectorySearcher search = new DirectorySearcher(entry) { Filter = ("(SAMAccountName=" + strUser + ")") }; 

     search.PropertiesToLoad.Add("givenName"); // First Name     
     search.PropertiesToLoad.Add("sn"); // Last Name 
     search.PropertiesToLoad.Add("cn"); // Last Name 

     result = search.FindOne(); 

     if (null == result) 
     { 
      return null; 
     } 

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

在上面的代碼段中,有沒有辦法檢索用戶的Windows登錄密碼,以便LDAP身份驗證在沒有再次詢問用戶密碼的情況下工作? 可以通過任何方式檢索正在創建DirectoryEntry對象時傳遞的「strPass」的值嗎?通過asp.net檢索windows登錄密碼

回答

2

密碼不存在任何地方。如果是的話,這將是一個很大的安全漏洞。

另外,順便說一句,擺脫try/catch塊。它什麼都不做,只是隱藏了例外的原因。

0

您可以在ASP.NET應用程序中設置Windows身份驗證。

http://msdn.microsoft.com/en-us/library/ff647405.aspx 設置完成後,只有經過身份驗證的用戶才能訪問您網站的受保護部分。

這使您可以訪問一些關鍵的信息位。

例如:

System.Web.HttpContext.Current.User.Identity.Name - 給出了認證的用戶的名稱(域\用戶名)。

System.Web.HttpContext.Current.User.IsInRole("role_name_here") - 會告訴你驗證的用戶是否在給定的角色中。

驗證可能會第一次很難 - 請不要詢問用戶的Windows密碼 - 這是一個安全風險 - 允許IIS和.NET框架爲您處理此問題。上面的文章可能有點長,看起來有點複雜,但它有很多很好的信息。