我在我的C#web应用程序中使用DirectoryEntry方法设置LDAP登录。下面的代码是我到目前为止。这将让任何拥有AD帐户的人登录。我需要将其限制在名为“commonusers”的组中。检查用户登录对AD组
public Boolean ValidateUser(string userName, string password)
{
string path = "LDAP://domain.company.org";
DirectoryEntry dirEntry = new DirectoryEntry(path, userName, password, AuthenticationTypes.Secure);
try
{
DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry);
dirSearcher.FindOne();
return true;
// If it returns the data then the User is validated otherwise it will automatically shift to catch block and return false
}
catch
{
return false;
}
登录按钮使用下面的代码:
protected void Button1_Click(object sender, EventArgs e)
{
{
Boolean boolresult = ValidateUser(TextBox_username.Text, TextBox_password.Text);
if (boolresult)
{
Label_loginstatus.Text = "Redirecting";
Response.Redirect("medsearch.aspx");
}
else
{
Label_loginstatus.Text = "Invalid username/password! Please try again.";
}
}
}
是否有可能增加一个检查功能的用户占了“commonusers”组到这些功能呢?
有没有需要添加的参考去这条路线?我添加了这个代码,并且PrincipalContext,ContextType,PrincipalSearcher,UserPrincipal和Principal都出现了错误。 –
System.DirectoryServices.AccountManagement - 睡前阅读这里:http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement(v = vs.110).aspx – Steen
因此,此代码工作筛选出用户不在组中,但它允许指定组中的用户无需密码登录... –