帮助使用目录服务帐户管理查找用户
我在尝试通过参数(用户名)将值传递到方法时遇到问题。如果我对值进行硬编码,它将找到用户。
任何指导将不胜感激,
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtUserName.Text))
{
string userName = txtUserName.ToString();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "car2.local", "DC=car2,DC=local");
UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);
if(usr != null)
{
lblStatus.Text = "user exists";
}
else
{
lblStatus.Text = "user does not exists";
}
}
}
I'm having an issue when trying to pass the value into the method via a parameter (userName). If I hard code the value, it will find the user.
Any guidance would be greatly appreciated,
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtUserName.Text))
{
string userName = txtUserName.ToString();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "car2.local", "DC=car2,DC=local");
UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);
if(usr != null)
{
lblStatus.Text = "user exists";
}
else
{
lblStatus.Text = "user does not exists";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个 - 读出文本框的
.Text
属性(并调用.Trim()
以消除任何额外的多余空格),而不是使用。 ToString()
:如果您没有指定要搜索的身份类型 - 那么 AD 将搜索最常见的身份类型并希望找到您的用户!
Try this - read out the
.Text
property of your textbox (and call.Trim()
to get rid of any additional, superfluous whitespace) instead of using.ToString()
on it:If you don't specify what type of identity to search for - then AD will search for the most common identity types and hopefully find your user!