Authenticate a Active directory user
//Authenticate a Active directory user
public static bool AuthenticateUser(string userName, string password, string domain)
{
bool isValidUser = false;
try
{
DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain, userName, password);
object nativeObject = entry.NativeObject;
isValidUser = true;
}
catch (DirectoryServicesCOMException ex)
{
throw new DirectoryServicesCOMException(ex.Message);
}
return isValidUser;
}
Add an User to Group
public static void AddUserToGroup(string UserName, string domain)
{
try
{
DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain);
entry.Properties["member"].Add(UserName);
entry.CommitChanges();
entry.Close();
}
catch (DirectoryServicesCOMException ex)
{
throw new DirectoryServicesCOMException(“Error while adding user to group “+ ex.Message);
}
}
Remove User from a Group
//Remove a User from a Active Directory Group
public static void RemoveUserFromGroup(string userName, string domain)
{
try
{
DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain);
entry.Properties["member"].Remove(userName);
entry.CommitChanges();
entry.Close();
}
catch (DirectoryServicesCOMException ex)
{
throw new DirectoryServicesCOMException(“Error while Remove user “ + ex.Message);
}
}
Create a User Account
//Create a userAccount
public static Guid CreateActiveDirUser(string userName, string password, string domain)
{
Guid oGuid = Guid.Empty;
try
{
DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain);
DirectoryEntry newUser = entry.Children.Add(“CN=” + userName, “user”);
newUser.Properties["samAccountName"].Value = userName;
newUser.CommitChanges();
oGuid = newUser.Guid;
newUser.Invoke(“SetPassword”, new Object[] { password });
newUser.CommitChanges();
newUser.Close();
}
catch (DirectoryServicesCOMException ex)
{
throw new DirectoryServicesCOMException(“Error while creating user “ + ex.Message);
}
return oGuid;
}
I am realy thank for you I found my need
thank’s again
By: sameer on February 3, 2009
at 2:06 pm
Really good one.
By: Pandu on April 14, 2009
at 2:45 pm