2013-02-21 105 views
1

我有一段時間試圖添加像部門和標題的字段。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 + +中,並沒有線索如何做到這一點。

任何幫助將是偉大的!謝謝!

回答

1

如果您使用.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