Programmatically Set NTFS File System Folder Permissions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HitSkiper

    #1

    Programmatically Set NTFS File System Folder Permissions

    I am getting the error "The security ID structure is invalid." When running
    this code.

    Sub SetPermissions( ByRef vPath As String, ByVal UserName As String)
    Dim objADsSec As ADsSecurity
    Dim objSecDes As SecurityDescrip tor
    Dim objDAcl As AccessControlLi st
    Dim objAce As Object
    Dim objAce1 As AccessControlEn try
    Dim objAce2 As AccessControlEn try
    Dim objSId As ADsSID
    Dim objSIdHex As Object

    UserName = "molnarc"
    objADsSec = New ADsSecurity
    objSecDes = CType(objADsSec .GetSecurityDes criptor("FILE://" &
    vPath), SecurityDescrip tor)
    objDAcl = CType(objSecDes .DiscretionaryA cl, AccessControlLi st)

    objSId = New ADsSID
    objSId.SetAs(AD SSECURITYLib.AD S_SID_FORMAT.AD S_SID_SAM,
    CStr(UserName))
    objSIdHex = objSId.GetAs(AD SSECURITYLib.AD S_SID_FORMAT.AD S_SID_SDDL)

    ' Add a new objAce so that the User has Full Control on NTFS Files.
    objAce1 = New AccessControlEn try
    objAce1.Trustee = CStr(objSIdHex)
    objAce1.AccessM ask = ActiveDs.ADS_RI GHTS_ENUM.ADS_R IGHT_GENERIC_AL L
    objAce1.AceType = ActiveDs.ADS_AC ETYPE_ENUM.ADS_ ACETYPE_ACCESS_ ALLOWED
    objAce1.AceFlag s = ActiveDs.ADS_AC EFLAG_ENUM.ADS_ ACEFLAG_INHERIT _ACE
    Or ActiveDs.ADS_AC EFLAG_ENUM.ADS_ ACEFLAG_INHERIT _ONLY_ACE Or 1
    objDAcl.AddAce( objAce1)

    ' Add a new objAce so that the User has Full Control on NTFS Folders.
    objAce2 = New AccessControlEn try
    objAce2.Trustee = CStr(objSIdHex)
    objAce2.AccessM ask = ActiveDs.ADS_RI GHTS_ENUM.ADS_R IGHT_GENERIC_AL L
    objAce2.AceType = ActiveDs.ADS_AC ETYPE_ENUM.ADS_ ACETYPE_ACCESS_ ALLOWED
    objAce2.AceFlag s = ActiveDs.ADS_AC EFLAG_ENUM.ADS_ ACEFLAG_INHERIT _ACE
    Or 1
    objDAcl.AddAce( objAce2)

    objSecDes.Discr etionaryAcl = objDAcl
    ' Set Permissions on the NTFS folder.
    objADsSec.SetSe curityDescripto r(objSecDes) '<-- Error happens here

    End Sub

    I have even tried this and still get the error.

    Sub SetPermissions( ByRef vPath As String, ByVal UserName As String)
    Dim objADsSec As ADsSecurity
    Dim objSecDes As SecurityDescrip tor
    Dim objDAcl As AccessControlLi st
    Dim objAce As Object
    Dim objAce1 As AccessControlEn try
    Dim objAce2 As AccessControlEn try
    Dim objSId As ADsSID
    Dim objSIdHex As Object

    UserName = "molnarc"
    objADsSec = New ADsSecurity
    objSecDes = CType(objADsSec .GetSecurityDes criptor("FILE://" &
    vPath), SecurityDescrip tor)
    objADsSec.SetSe curityDescripto r(objSecDes) '<-- Error happens here

    end sub

    Any clue as what i could be doing wrong?




Working...