0

I need to verify that a provided username is a Domain Administrator in c#. Any idea's on how to do this?

John
  • 4,658
  • 2
  • 14
  • 23
User
  • 351
  • 1
  • 8
  • 20

2 Answers2

0

You can use WindowsIdentity to get the current user.

Then create a WindowsPrincipal with the WindowsIdentity.

And check WindowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator)

Hope this can help you.

EDIT : I just see the ASP tag now... This link could help you, same thing but for ASP.

Community
  • 1
  • 1
TheMightyX2Y
  • 1,473
  • 1
  • 16
  • 24
0

Function 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

End Function

Function 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

End Function

Function 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

End Function

function 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

end function

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
KWallace
  • 1,570
  • 1
  • 15
  • 25