I have an form in my page that user informs Email or User Name for other user. In my Submit event, I need check that this information exists in Active Directory. How I make this check?? I'm clear?
My page
<asp:Label ID="lblUserAdd" runat="server" Font-Bold="true" Text="Add User - (Email or User Name)"></asp:Label>
<br />
<asp:TextBox ID="txtUserAdd" runat="server" Height="17px" Width="150px"></asp:TextBox>
<asp:Label ID="lblError" runat="server" class="control-label" for="inputError" Visible="false">Input with error</asp:Label>
<asp:Label ID="lblsuccess" runat="server" class="control-label" for="inputSuccess"
Visible="false">Input with success</asp:Label>
<asp:Button ID="btnAddUser" class="btn" runat="server" Font-Bold="true" Text="Add User"
OnClick="btnSendUser_OnClick" />
<br />
<table id="tblUsers" class="table table-bordered">
<asp:Label ID="lblUser" runat="server" Visible="false"></asp:Label>
</table>
My .cs
protected void btnSendUser_OnClick(object sender, EventArgs e)
{
PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain, "x.com", "amsndruser", "x");
lblUser.Visible = true;
lblUser.Text = txtUserAdd.Text;
if (txtUserAdd.Text != "EMAIL AD" || txtUserAdd.Text != "USER NAME AD" || txtUserAdd.Text != "USER AD")
{
lblError.Visible = true;
}
else
{
lblsuccess.Visible = true;
}
}
I create an solution for this.
protected void btnSendUser_OnClick(object sender, EventArgs e)
{
string Loginfo = txtUserAdd.Text;
string LoginInfo = txtUserAdd.Text;
PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "x.com", "amsndruser", "XXX");
UserPrincipal insUserPrincipal = UserPrincipal.FindByIdentity(insPrincipalContext, LoginInfo);
if (insUserPrincipal == null)
{
lblError.Visible = true;
}
else
{
lblsuccess.Visible = true;
lblUser.Visible = true;
lblUser.Text = txtUserAdd.Text;
}
}
I know, it is really old thread, but I was searching for a solution and finally, this works for me:
using System.DirectoryServices.AccountManagement;
static bool UserExists(string UserName, string Email)
{
PrincipalContext context = new PrincipalContext(ContextType.Domain, "YourDomain.xxx");
UserPrincipal user = new UserPrincipal(context);
user.SamAccountName = UserName;
user.EmailAddress = Email;
PrincipalSearcher ps = new PrincipalSearcher();
ps.QueryFilter = user;
var results = ps.FindAll().ToList();
return results.Any();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.