问题描述
我正在使用 System.DirectoryServices.AccountManagement 命名空间类在 AD 中添加和管理用户,但我似乎无法找到如何向用户对象添加地址信息.我正在使用 UserPrincipal 类以编程方式将用户添加到 AD.
有什么想法吗?
解决方案
以下是使用可扩展性调用实现此目的的示例:
类 DSPrincipals{静态无效主(字符串 [] args){/* 检索主体上下文*/PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "ou=Monou,dc=dom,dc=fr", "jpb", "pass@1w0rd01");/* 创建一个用户主体对象*/slxUser aSlx??User = new slxUser(domainContextMonou, "W.Zeidan", "pass@1w0rd01", true);/* 将一些属性分配给用户主体*/aSlxUser.GivenName = "Wessam";aSlxUser.Surname = "Zeidan";aSlxUser.streetAddress = "Add1";/* 强制用户在下次登录时更改密码*/aSlxUser.ExpirePasswordNow();/* 将用户保存到目录*/aSlxUser.Save();Console.ReadLine();}}[目录对象类(用户")][DirectoryRdnPrefix("CN")]类 slxUser : UserPrincipal{公共 slxUser(PrincipalContext 上下文):基础(上下文){}public slxUser(PrincipalContext context, string samAccountName, string password, bool enabled ) : base(context, samAccountName, password, enabled){}[DirectoryProperty("streetAddress")]公共字符串 streetAddress{得到{object[] result = this.ExtensionGet("streetAddress");如果(结果!= null){返回(字符串)结果[0];}别的{返回空;}}set { this.ExtensionSet("streetAddress", value);}}}
您可以在 MSDN 文档中找到更多信息.>
结果如下:
I'm using System.DirectoryServices.AccountManagement namespace classes to add and manage users in AD, but I can't seem to find how to add Address information to user objects. I'm using the UserPrincipal class to add users programatically to AD.
Any ideas?
解决方案
Here is a sample to do that by using extensibility call :
class DSPrincipals { static void Main(string[] args) { /* Retreiving a principal context */ PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "ou=Monou,dc=dom,dc=fr", "jpb", "pass@1w0rd01"); /* Create a user principal object */ slxUser aSlxUser = new slxUser(domainContextMonou, "W.Zeidan", "pass@1w0rd01", true); /* assign some properties to the user principal */ aSlxUser.GivenName = "Wessam"; aSlxUser.Surname = "Zeidan"; aSlxUser.streetAddress = "Add1"; /* Force the user to change password at next logon */ aSlxUser.ExpirePasswordNow(); /* save the user to the directory */ aSlxUser.Save(); Console.ReadLine(); } } [DirectoryObjectClass("user")] [DirectoryRdnPrefix("CN")] class slxUser : UserPrincipal { public slxUser(PrincipalContext context) : base(context) { } public slxUser(PrincipalContext context, string samAccountName, string password, bool enabled ) : base(context, samAccountName, password, enabled) { } [DirectoryProperty("streetAddress")] public string streetAddress { get { object[] result = this.ExtensionGet("streetAddress"); if (result != null) { return (string)result[0]; } else { return null; } } set { this.ExtensionSet("streetAddress", value); } } }
You'll find more information in MSDN documentation.
Here is the result :