目前我有一种方法来检查用户是否经过身份验证,但我希望我的 ASP.net 应用程序使用默认登录名实现 ActiveDirectory 身份验证。
我目前的方法:
public bool IsAuthenticated(string user, string pass)
{
bool authenticated = false;
string path = "LDAP://my path here";
DirectoryEntry adsEntry = new DirectoryEntry(path);
adsEntry.AuthenticationType = AuthenticationTypes.Secure;
adsEntry.Username = user;
adsEntry.Password = pass;
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
adsSearcher.Filter = "(sAMAccountName=" + user + ")";
try
{
SearchResult adsSearchResult = adsSearcher.FindOne();
authenticated = true;
adsEntry.Close();
}
catch (Exception ex)
{
// Failed to authenticate. Most likely it is caused by unknown user
// id or bad strPassword.
//strError = ex.Message;
adsEntry.Close();
}
return authenticated;
尝试在 web.config 中实现登录功能,我写了以下内容:
<membership defaultProvider="MembershipADProvider">
<providers>
<add
name="MembershipADProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ADConnectionString"
/>
</providers>
</membership>
它似乎正在连接到 LDAP 服务器,因为它会抛出一个Bad username or password valdiation error
. 另一方面,我不确定它是否甚至连接到服务器,因为该服务器在 3 次不正确的身份验证尝试后阻止了其他每个应用程序上的用户,而这并没有发生。我不确定是否必须将属性添加connectionUsername
到connectionPassword
web.config 或让Login
命令在登录时用每个用户名/密码填充它们。任何帮助,将不胜感激。