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”的值吗?

4

3 回答 3

2

密码在任何地方都不存在。如果是这样,那将是一个很大的安全漏洞。

另外,顺便说一句,摆脱 try/catch 块。它除了隐藏异常的原因之外什么也没做。

于 2011-05-18T16:20:51.380 回答
0

使用ActiveDirectoryMemebershipProvider - 您可以在不编写代码的情况下进行身份验证,从而消除您当前拥有的登录场景。

于 2011-05-18T16:21:22.937 回答
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 框架为您处理这个问题。上面的文章可能有点长,看起来有点复杂,但是里面有很多很好的信息。

于 2011-05-18T17:53:57.813 回答