2011-05-18 81 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框架为您处理此问题。上面的文章可能有点长,看起来有点复杂,但它有很多很好的信息。