【问题标题】:How to: Check if current user is member of ‘domain admins’如何:检查当前用户是否是“域管理员”的成员
【发布时间】:2014-03-21 07:19:29
【问题描述】:

我需要验证提供的用户名是 c# 中的域管理员。 关于如何做到这一点的任何想法?

【问题讨论】:

  • 您尝试过任何方法来解决您的问题吗?
  • 是的,我的意思是 asp .net c#

标签: c# asp.net


【解决方案1】:

您可以使用WindowsIdentity 获取当前用户。

然后使用WindowsIdentity 创建一个WindowsPrincipal

然后检查WindowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator)

希望对你有帮助。

编辑:我现在才看到 ASP 标签...这个link 可以帮助你,同样的事情,但对于 ASP。

【讨论】:

    【解决方案2】:

    函数 ADUserInfo(sLogonUser, cOption)

    Dim oConnection
    Dim oCommand
    Dim oRoot
    Dim oDomain
    Dim sADsPath
    Dim sDomain
    
    sDomain = Mid(sLogonUser, 1, Instr(1, sLogonUser, "\") - 1)
    
    Set oConnection = CreateObject("ADODB.Connection")
    With oConnection
        .Provider = "ADsDSOObject"
        .Mode = "1" 'Read
        .Properties("Encrypt Password") = True 
        .Open "Active Directory Provider"
    End With
    
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    
    Set oRoot = GetObject("LDAP://" & sDomain & "/rootdse")
    Set oDomain = GetObject("LDAP://" & sDomain & "/" & oRoot.Get("defaultNamingContext"))
    sADsPath = "<" & oDomain.ADsPath & ">"
    
    
    Select Case lcase(cOption) 
    
        Case "groups"
    
            ADUserInfo = ADUserGroups(sLogonUser, oConnection, oCommand, oRoot, oDomain, sADsPath)
    
        Case "name"
    
            ADUserInfo = ADUserName(sLogonUser, oConnection, oCommand, oRoot, oDomain, sADsPath)
    
        Case "supervisor"
    
    
    End Select
    

    结束函数

    函数 ADUserGroups(sLogonUser, oConnection, oCommand, oRoot, oDomain, sADsPath)

    Dim sFilter
    Dim sAttribsToReturn
    Dim sDepth
    Dim sDomainSID
    Dim vObjectSID
    Dim sObjectSID
    Dim sGroupRID
    Dim iPrimaryGroupID
    Dim oPrimaryGroup
    Dim oRS
    
    Dim value
    Dim cGroups
    Dim sDomain
    Dim sLogonName
    
    sDomain = Mid(sLogonUser, 1, Instr(1, sLogonUser, "\") - 1)
    sLogonName = Mid(sLogonUser, Instr(1, sLogonUser, "\") + 1)
    
    sFilter = "(&(objectCategory=Person)(objectClass=user)(sAMAccountName=" & sLogonName & "))"
    sAttribsToReturn = "memberOf,primaryGroupID,objectSID"
    sDepth = "subTree"
    
    ocommand.CommandText = sADsPath & ";" & sFilter & ";" & sAttribsToReturn & ";" & sDepth
    
    Set oRS = ocommand.Execute
    
    ' Only one user should meet the criteria
    If (oRS.RecordCount = 1) Then
    
        ' Get that user's info
        For i = 0 To oRS.Fields.Count - 1
    
            If (oRS.Fields(i).Name = "memberOf") Then
                ' I've never seen this field come back with more than 
                ' ONE value, but the original code I started with 
                ' treated the memberOf property as though it was a 
                ' collection.  So, I've left it a collection until 
                ' I can verify it.  KLW
    
                cGroups = ""
                For Each value In oRS.Fields(i).Value
                    cGroups = cGroups & replace(split(value,",")(0),"CN=","") & ";"
                Next
    
            ElseIf (oRS.Fields(i).Name = "primaryGroupID") Then
                ' need this to get the PrimaryGroup after other group membership has been obtained
                ' (Primary Group ID and Object SID ID needed to get the primary group)
                iPrimaryGroupID = oRS.Fields(i).Value
    
            ElseIf (oRS.Fields(i).Name = "objectSID") Then
                ' adVarBinary -- need this to get the PrimaryGroup.  
                ' It is not included in the memberOf group list
    
                vObjectSID = oRS.Fields(i).Value
                sObjectSID = SDDL_SID(vObjectSID)
            End If
        Next
    
        ' The primary group is not included in memberOf...
    
        ' We have the SDDL form of the user's SID.
        ' Remove the user's RID ( the last sub authority)
        ' up to the "-"
        '
        sDomainSID = Mid(sObjectSID, 1, (InStrREV(sObjectSID,"-")))
    
        ' Build the SID of the Primary group
        ' from the domainSID and the Primary Group RID in
        ' the PrimaryGroupID.
        '
        sGroupRID = StrRID(iPrimaryGroupID)
        sDomainSID = sDomainSID & sGroupRID
    
        ' Get the primary group   
        '
        set oPrimaryGroup = GetObject("LDAP://" & sDomain & "/<SID=" & sDomainSID & ">")
    
        cGroups = replace(split(oPrimaryGroup.Get("DistinguishedName"),",")(0),"CN=","") & ";" & cGroups
    
        ADUserGroups = cGroups
    
    End If
    

    结束函数

    函数 ADUserName(sLogonUser, oConnection, oCommand, oRoot, oDomain, sADsPath)

    Dim sFilter
    Dim sAttribsToReturn
    Dim sDepth
    Dim sDomainSID
    Dim vObjectSID
    Dim sObjectSID
    Dim sGroupRID
    Dim iPrimaryGroupID
    Dim oPrimaryGroup
    Dim oRS
    
    Dim value
    Dim sDomain
    Dim sLogonName
    
    sDomain = Mid(sLogonUser, 1, Instr(1, sLogonUser, "\") - 1)
    sLogonName = Mid(sLogonUser, Instr(1, sLogonUser, "\") + 1)
    
    
    sFilter = "(&(objectCategory=Person)(objectClass=user)(sAMAccountName=" & sLogonName & "))"
    sAttribsToReturn = "distinguishedName"
    sDepth = "subTree"
    ocommand.CommandText = sADsPath & ";" & sFilter & ";" & sAttribsToReturn & ";" & sDepth
    
    Set oRS = ocommand.Execute
    
    ' Only one user should meet the criteria
    If (oRS.RecordCount = 1) Then
    
        ' Get that user's info
        For i = 0 To oRS.Fields.Count - 1
    
            If (oRS.Fields(i).Name = "distinguishedName") Then
                ADUserName = replace(split(oRS.Fields(i).Value,",")(0),"CN=","")
            End If
        Next
    
    
    End If
    

    结束函数

    函数 SDDL_SID (oSID)

    dim IssueAuthorities(11)
    Dim SubAuthorities
    Dim strSDDL
    Dim IssueIndex
    Dim Revision
    Dim i, j, k, index, p2, subtotal, dblSubAuth
    
    IssueAuthorities(0) = "-0-0"
    IssueAuthorities(1) = "-1-0"
    IssueAuthorities(2) = "-2-0"
    IssueAuthorities(3) = "-3-0"
    IssueAuthorities(4) = "-4"
    IssueAuthorities(5) = "-5"
    IssueAuthorities(6) = "-?"
    IssueAuthorities(7) = "-?"
    IssueAuthorities(8) = "-?"
    IssueAuthorities(9) = "-?"
    
    ' First byte is the revision value
    '
    Revision = ascb(midB(osid,1,1))
    
    ' Second byte is the number of sub authorities in the
    ' SID
    '
    SubAuthorities = CInt(ascb(midb(oSID,2,1)))
    strSDDL = "S-" & Revision
    IssueIndex = CInt(ascb(midb(oSID,8,1)))
    
    strSDDL = strSDDL & IssueAuthorities(IssueIndex)
    
    index = 9
    i = index
    for k = 1 to SubAuthorities 
    
        p2 = 0
        subtotal = 0
        for j = 1 to 4
            dblSubAuth = CDbl(ascb(midb(osid,i,1))) * (2^p2)
            subTotal = subTotal + dblSubAuth
            p2 = p2 + 8
            i = i + 1
        next
    
        ' Convert the value to a string, add it to the SDDL Sid and continue
        '
        strSDDL = strSDDL & "-" & cstr(subTotal)
    next
    SDDL_SID = strSDDL
    

    结束函数

    function Get_HexString( oSID )
    
    Dim outStr, i, b
        outStr = ""
        for i = 0 to Ubound(oSid)
            b = hex(ascb(midb(oSid,i+1,1)))
            if( len(b) = 1 ) then b = "0" & b
            outStr = outStr & b
        next
        Get_HexString = outStr
    end function
    
    function StrRID( inVal )
        dim dLocal
        if( (inVal and &H80000000) <> 0 ) then
            dLocal = CDbl((inval and &H7FFFFFFF))
            dLocal = dLocal + 2^31
            StrRID = cstr(dLocal)
        else
            StrRID = Cstr(inVal)
        end if
    end function
    

    【讨论】:

      猜你喜欢
      • 2011-04-05
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      相关资源
      最近更新 更多