Search This Blog

Total Pageviews

Wednesday, April 18, 2012

AD User Login

AD User Login


protected void btnLogin_Click(object sender, EventArgs e)
{
string domainname = "domainname";
try
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainname))
{
// validate the credentials
bool isValid = pc.ValidateCredentials(txtUserName.Text.Trim(), txtPassword.Text.Trim());
if (isValid)
{
UserPrincipal user = UserPrincipal.FindByIdentity(pc, txtUserName.Text.Trim());
lblstatus.Text = "Welcome " + user.Name + ",";
}
else
{
lblstatus.Text = "Invalid Username or Password.";
}
}
}
catch (Exception ex)
{
lblstatus.Text = "Invalid Domain";
}
}

Wednesday, April 4, 2012

LDAP User Search

lstbox1.Items.Clear();

DirectoryEntry myOU = new DirectoryEntry("LDAP://DC=msbu,DC=sharepoint,DC=com");DirectorySearcher userSearcher = new DirectorySearcher(myOU);//userSearcher.Filter = "(objectClass=user)";userSearcher.Filter = String.Format("( &(objectClass=user) (name={0}))", txtUserName.Text.Trim() + "*");//Label1.Text = userSearcher.FindAll().Count.ToString ();
{

lstbox1.Items.Add(
}
foreach (SearchResult srchresult in userSearcher.FindAll())if (srchresult.Properties["name"].Count > 0)new ListItem(Convert.ToString(srchresult.Properties["name"][0])));