【问题标题】:Delete User from AD Group从 AD 组中删除用户
【发布时间】:2010-10-06 16:15:04
【问题描述】:

我正在尝试通过代码从 Active Directory 组中删除用户。得到有用的错误:

目标已抛出异常 调用”

跟踪:堆栈跟踪:在 System.DirectoryServices.DirectoryEntry.Invoke(字符串 方法名,对象 [] 参数)在 Active_Directory.RemoveUserFromGroup(字符串 sInUserName,字符串 sInGroupName) 在 C:\文档和设置\用户\我的 文档\Visual Studio 2010\WebSites\appname\App_Code\Common\Active_Directory.vb:line 192

这是我的功能:

查看调用行:oGroup.Invoke("Remove", New Object() {oUser.Path})

Public Shared Sub RemoveUserFromGroup(ByVal sInUserName As String _
                                      , ByVal sInGroupName As String)
    Dim entry1 As DirectoryEntry
    Dim de As DirectoryEntry
    Dim deSearch As DirectorySearcher
    Dim results As SearchResult
    Dim comeon As String
    Dim oUser As DirectoryEntry
    Dim oGroup As DirectoryEntry
    Dim sr As SearchResult

    Try

        entry1 = New DirectoryEntry("LDAP://rootDSE")
        comeon = entry1.Properties("DefaultNamingContext").Item(0)
        de = New DirectoryEntry("LDAP://" & comeon)

        deSearch = New DirectorySearcher()
        deSearch.SearchRoot = de
        deSearch.Filter = "(sAMAccountName=" + sInUserName + ")"
        deSearch.PropertiesToLoad.Add("cn")
        sr = deSearch.FindOne()

        If sr Is Nothing Then
            oUser = Nothing
        Else
            oUser = sr.GetDirectoryEntry()
        End If

        deSearch.Dispose()
        deSearch = Nothing
        sr = Nothing

        If Not (oUser Is Nothing) Then

            deSearch = New DirectorySearcher()
            deSearch.SearchRoot = de
            deSearch.Filter = "(&(objectClass=group) (CN=" & sInGroupName & "))"
            deSearch.SearchScope = SearchScope.Subtree

            results = deSearch.FindOne()

            If results IsNot Nothing Then

                oGroup = results.GetDirectoryEntry()

                Try

                    oGroup.Invoke("Remove", New Object() {oUser.Path})
                    oGroup.CommitChanges()
                    oGroup.Close()

                Catch ex As Exception
                    Dim s As String
                    s = ex.ToString
                    s = ""
                End Try

            End If

            entry1.Dispose()
            de.Dispose()

            entry1 = Nothing
            de = Nothing
            deSearch = Nothing
            results = Nothing

        End If

        oUser.Close()

    Catch ex As Exception

        Dim myerror As New MyError
        myerror.showMeTheError(ex)

    End Try

End Sub

【问题讨论】:

    标签: .net asp.net active-directory ldap ldap-query


    【解决方案1】:

    你似乎做的非常复杂——没有必要这样做。

    查看 Howto do almost everything in Active Directory CodeProject 文章 - 很棒的东西。

    这是从组(也由 DN 定义)中删除用户(由他的 DN 给出)所需的 sn-p:

    public void RemoveUserFromGroup(string userDn, string groupDn)
    {
        try
        {
            DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
            dirEntry.Properties["member"].Remove(userDn);
            dirEntry.CommitChanges();
            dirEntry.Close();
        }
        catch (System.DirectoryServices.DirectoryServicesCOMException E)
        {
            //doSomething with E.Message.ToString();
    
        }
    }
    

    这对你有用吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2018-05-24
      • 1970-01-01
      相关资源
      最近更新 更多