找回密码
 注册

QQ登录

只需一步,快速开始

BCM 门户 IT世界 应用开发 查看内容

Active Directory via C#.Net 3.5

2012-2-6 10:04| 发布者: Test| 查看: 1822| 评论: 0|原作者: Raymund Macaalay|来自: Raymund Macaalay's Dev Blog

摘要: Before .Net managing Active Directory objects is a bit lengthy and you need a good knowledge on the principal store to have your head around on what you want to do. We ususally use the System.Director ...

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/


1234

鲜花

握手

雷人

路过

鸡蛋

最新评论

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-11 11:34 , Processed in 0.035927 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

返回顶部