Problem Checking Write Permissions on file using impersonation context

C0d3M0nk3y

Member
Joined
May 6, 2011
Messages
10
Programming Experience
Beginner
I have created a click once application that is designed to look for a specific xml config file and modify several values in the file if it is foud and if the user has access to write to the file. The xml modification code works the problem I am having is testing and generating an error if the user does not have write access to the file.

VB.NET:
Public Shared Function getFtpServerValue() As String
        Dim ecwConfigFileTarget As String = (Environment.GetEnvironmentVariable("CW_HOME", EnvironmentVariableTarget.Machine) & "\configuration.xml")
        Dim configExist As String = ecwConfigFileTarget
        If (File.Exists(configExist)) Then
        Else : MsgBox("The config file does not exist. Please contact support.", vbCritical, "Failed")
        End If
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Dim user As WindowsPrincipal = CType(System.Threading.Thread.CurrentPrincipal, WindowsPrincipal)
        Dim ident As WindowsIdentity = CType(user.Identity, WindowsIdentity)
        Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
        impersonationContext = ident.Impersonate()
        Using impersonationContext
            Try
                Dim filePermission As FileIOPermission
                filePermission = New FileIOPermission(FileIOPermissionAccess.Write, ecwConfigFileTarget)
                filePermission.Demand()
            Catch ex As SecurityException
                MsgBox("Your account does not have write access to the config file. Please contact support.", vbCritical, "Failed")
            End Try
        End Using
        impersonationContext.Undo()
        Dim ecwConfigFile As New XmlDocument()
        ecwConfigFile.Load(ecwConfigFileTarget)
        Dim ecwFtpValue As XmlNode
        ecwFtpValue = ecwConfigFile.SelectSingleNode("//ftpserver")
        getFtpServerValue = LCase(ecwFtpValue.InnerXml)
    End Function

The impersonation works however the file IO permission sectionis what I can't get to work correctly. I have testing this on a config file that I have verified the user does not have access to write to.
any help would be appreciated.
 
Back
Top