通过 asp.net 检索Windows登录密码
本文关键字:登录 密码 Windows 检索 asp net 通过 | 更新日期: 2023-09-27 17:56:56
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"的值?
密码在
任何地方都不存在。如果是这样,那将是一个很大的安全漏洞。
另外,顺便说一句,摆脱尝试/捕获块。它除了隐藏异常的原因外什么也没做。
使用 ActiveDirectoryMemebershipProvider - 无需编写代码即可进行身份验证,从而消除了当前拥有的登录方案。
你可以在 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 框架为您处理此问题。上面的文章可能有点长,看起来有点复杂,但它有很多很好的信息。