Now this is how to use it.
ADMethodsAccountManagement ADMethods = new ADMethodsAccountManagement();
UserPrincipal myUser = ADMethods.GetUser("Test");
myUser.GivenName = "Given Name";
myUser.Surname = "Surname";
myUser.MiddleName = "Middle Name";
myUser.EmailAddress = "Email Address";
myUser.EmployeeId = "Employee ID";
myUser.Save();
------------------
If you have noticed there is no more
attribute editing function on the library I created as it is now exposed
on the UserPrincipal namespace itself so if you want to update
information such as name, last name, employee id as some samples you can
do it by searching for the user like this
ADMethodsAccountManagement ADMethods = new ADMethodsAccountManagement();
UserPrincipal myUser = ADMethods.GetUser("your Username");
and assigning values to it like such:
myUser.GivenName = "Given Name";
myUser.Surname = "Surname";
myUser.MiddleName = "Middle Name";
myUser.EmailAddress = "Email Address";
myUser.EmployeeId = "Employee ID";
myUser.Save();
--------------------------
You can use this two methods RemoveUserFromGroup and AddUserToGroup, the
parameters are only User Name and Group Name, if it doesnt find any it
returns a boolean variable. ---------------------------------------------------------- Kishor says:
I am trying to hook up with Active directory to get the Group of the user using System.DirectoryServices.AccountManagement.
When I am running the Application from VS2010 it Authenticate user and Display the Group as well.
But when I am browsing application from IIS 5.1 it showing error while checking the group.
“System.DirectoryServices.DirectoryServicesCOMException: Logon failure: unknown user name or bad password.”
PrincipalContext pc = new PrincipalContext(ContextType.Domain, “TEST1″)
On Sentence
UserPrincipal.FindByIdentity(pc, userName);
Kindly provide some help. Please find the code of GetPrincipalContect as given below.
public PrincipalContext GetPrincipalContext()
{
//PrincipalContext oPrincipalContext = new
PrincipalContext(ContextType.Domain, sDomain, sDefaultOU,
ContextOptions.SimpleBind, sServiceUser, sServicePassword);
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, “TEST1″);
return oPrincipalContext;
}
rsmacaalay says:
Looks like you are using
PrincipalContext(ContextType, String) where the or your “TEST1″ will be
the name of the domain or server hosting the ApplicationDirectory
instance. Which means the application will set the default username and
password when the instance of the Principal Cotnext initializes, this
will be fine on your machine as it will inherit your user who is logged
in to the machine but when using IIS if you are not using Integrated
Windows Authentication with the Anonymous Access off then it would use
the IUSR_SERVERNAME account to initiallize the Principal Cotnext, but if
you are using the the Integrated Windows Authentication with the
Anonymous Access off can you do a Response.Write of the username the
site is running as.
Kishor says:
Hi,
I tried to get the username by WindowsIdentity.GetCurrent().Name.ToString()
When I am browsing application from IIS ( Integrated Windows Authentication with the Anonymous Access off )
and try to get the user name it showing “PC_NAME\ASPNET”
and
When I am running from VS2010 it showing “DOMAIN\MyUserId”
who is logged in to the machine. Hi this has been resolved.
When code runs localy using VS2010 it run under my conditionals (user Logon on PC)
And when it runs from IIS it runs under a small right account.
So I tried impersonate tag in Web.Config with username and password
And it worked proper.
Thanks You very Mush rsmacaalay for your suggession.
http://anyrest.wordpress.com/2010/06/28/active-directory-c/
|
If you have noticed there is no more attribute editing function on the library I created as it is now exposed on the UserPrincipal namespace itself so if you want to update information such as name, last name, employee id as some samples you can do it by searching for the user like this
ADMethodsAccountManagement ADMethods = new ADMethodsAccountManagement();UserPrincipal myUser = ADMethods.GetUser("your Username");
and assigning values to it like such:
myUser.GivenName = "Given Name";myUser.Surname = "Surname";
myUser.MiddleName = "Middle Name";
myUser.EmailAddress = "Email Address";
myUser.EmployeeId = "Employee ID";
myUser.Save();
--------------------------You can use this two methods RemoveUserFromGroup and AddUserToGroup, the parameters are only User Name and Group Name, if it doesnt find any it returns a boolean variable.
----------------------------------------------------------
Kishor says:
I am trying to hook up with Active directory to get the Group of the user using System.DirectoryServices.AccountManagement.
When I am running the Application from VS2010 it Authenticate user and Display the Group as well.
But when I am browsing application from IIS 5.1 it showing error while checking the group.
“System.DirectoryServices.DirectoryServicesCOMException: Logon failure: unknown user name or bad password.”
PrincipalContext pc = new PrincipalContext(ContextType.Domain, “TEST1″)
On Sentence
UserPrincipal.FindByIdentity(pc, userName);
Kindly provide some help.
Please find the code of GetPrincipalContect as given below.
public PrincipalContext GetPrincipalContext()
{
//PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, “TEST1″);
return oPrincipalContext;
}
rsmacaalay says:
Looks like you are using PrincipalContext(ContextType, String) where the or your “TEST1″ will be the name of the domain or server hosting the ApplicationDirectory instance. Which means the application will set the default username and password when the instance of the Principal Cotnext initializes, this will be fine on your machine as it will inherit your user who is logged in to the machine but when using IIS if you are not using Integrated Windows Authentication with the Anonymous Access off then it would use the IUSR_SERVERNAME account to initiallize the Principal Cotnext, but if you are using the the Integrated Windows Authentication with the Anonymous Access off can you do a Response.Write of the username the site is running as.
Kishor says:
Hi,
I tried to get the username by WindowsIdentity.GetCurrent().Name.ToString()
When I am browsing application from IIS ( Integrated Windows Authentication with the Anonymous Access off )
and try to get the user name it showing “PC_NAME\ASPNET”
and
When I am running from VS2010 it showing “DOMAIN\MyUserId”
who is logged in to the machine.
Hi this has been resolved.
When code runs localy using VS2010 it run under my conditionals (user Logon on PC)
And when it runs from IIS it runs under a small right account.
So I tried impersonate tag in Web.Config with username and password
And it worked proper.
Thanks You very Mush rsmacaalay for your suggession.
http://anyrest.wordpress.com/2010/06/28/active-directory-c/