向 Active Directory 用户添加地址信息
- 作者: 哥布拉Plus
- 来源: 51数据库
- 2022-10-21
问题描述
我正在使用 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 :
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
