VB.net使用UserPrincipalEx创建新的AD用户帐户?

问题描述:

我有一段时间试图添加像部门和标题的字段。VB.net使用UserPrincipalEx创建新的AD用户帐户?

我用它来创建用户帐户:

Dim ctx As New PrincipalContext(ContextType.Domain, "domain.name.pvt", "OU=Users,DC=global,DC=pvt") 

Dim usr As UserPrincipal = New UserPrincipal(ctx) 

我没有问题,在创建帐户,但不能添加简单的事情,像DepartmentTitle。我阅读了关于使用扩展,但在C + +中,并没有线索如何做到这一点。

任何帮助将是伟大的!谢谢!

如果您使用.NET 3.5及更高版本,则应检查System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。阅读所有关于它在这里:

要扩展UserPrincipal类,你并不需要太多的 - 这样的事情就足够了(我在C#最初写这一点,只是将其转换为VB.NET上的网络 - 我希望VB.NET代码没有问题!

Imports System.DirectoryServices.AccountManagement 

Namespace ADExtended 
    <DirectoryRdnPrefix("CN")> _ 
    <DirectoryObjectClass("Person")> _ 
    Public Class UserPrincipalEx 
     Inherits UserPrincipal 
     ' Inplement the constructor using the base class constructor. 
     Public Sub New(context As PrincipalContext) 
      MyBase.New(context) 
     End Sub 

     ' Implement the constructor with initialization parameters.  
     Public Sub New(context As PrincipalContext, samAccountName As String, password As String, enabled As Boolean) 
      MyBase.New(context, samAccountName, password, enabled) 
     End Sub 

     ' Create the "Department" property.  
     <DirectoryProperty("department")> _ 
     Public Property Department() As String 
      Get 
       If ExtensionGet("department").Length <> 1 Then 
        Return String.Empty 
       End If 

       Return DirectCast(ExtensionGet("department")(0), String) 
      End Get 
      Set 
       ExtensionSet("department", value) 
      End Set 
     End Property 

     ' Create the "Title" property.  
     <DirectoryProperty("title")> _ 
     Public Property Title() As String 
      Get 
       If ExtensionGet("title").Length <> 1 Then 
        Return String.Empty 
       End If 

       Return DirectCast(ExtensionGet("title")(0), String) 
      End Get 
      Set 
       ExtensionSet("title", value) 
      End Set 
     End Property 

     ' Implement the overloaded search method FindByIdentity. 
     Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityValue As String) As UserPrincipalEx 
      Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityValue), UserPrincipalEx) 
     End Function 

     ' Implement the overloaded search method FindByIdentity. 
     Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityType As IdentityType, identityValue As String) As UserPrincipalEx 
      Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityType, identityValue), UserPrincipalEx) 
     End Function 
    End Class 
End Namespace 

现在,你只需要使用UserPrincipalEx类:

Dim ctx As New PrincipalContext(ContextType.Domain, "domain.name.pvt", "OU=Users,DC=global,DC=pvt") 

Dim usr As UserPrincipalEx = New UserPrincipalEx(ctx) 
usr.Title = "......." 
usr.Department = "......." 

新S.DS.AM使得它可以很容易在公元玩弄用户和组!

+0

谢谢!我会尝试。我是任何名字空间的新手。我会努力工作,谢谢嘘! – 2013-02-25 14:51:59

+0

谢谢,我没有得到它的工作:(boo :( 当我在代码中输入它说太多的参数在公共小组新()。多数民众赞成在消息时,我将鼠标悬停在(ctx) – 2013-02-25 15:01:29

+0

你试过创建一个UserPrincipalEx类的新用户?我收到错误说“服务器不愿处理请求”。其他功能似乎很适合更新部门或员工ID。 – Force 2014-09-08 19:39:06