System.security.permissions.securitypermission, mscorlib error

I have written some ASP.NET code to create a new folder on the remote server when a new customer registers.

The idea is that all the new customers uploaded files will reside in his/her folder…

When I run the registration process on my local development machine everything works fine, once the files are uploaded and remote server folder permissions are set then it bombs out….

My hosting service provider provided the following sample code and information…

Hosting / Scripting & Programming / ASP.NET
Create A New Directory Using ASP.NET

Sample Code....
<%@ Import Namespace="System.IO"%>
<html>
 <head>
  <title>File Download</title>
  <script language="VB" runat="server">
  Sub createFolder(sender As Object, e As EventArgs)
    If ((dirname.Text) = "") Then
      message1.Text = "Error: you must enter directory name"
    Else 
      Directory.CreateDirectory(Server.MapPath("./" + dirname.Text))
      message1.Text = "Directory successfully created"
    End If 
  End Sub
  </script>
  </head>
<body>
<form runat="server">
DirName: 
  <asp:Textbox id=dirname size=14 runat=server /><br/>
  <asp:Button Text="Create Directory" OnClick="createFolder" Runat="Server"/><br/>
  <asp:Label id="message1" Runat="Server"/><br/>
  <asp:Label id="Folders" EnableViewState="False" Runat="Server"/><br/>
</form>
</body>
</html>

Additional Information:
You have to change file/folder permissions using the server app to grant write permissions to the folder where the script is saved. Please make sure that both IUSR and NETWORK should have write permissions. You will also have to ensure the following line is present in your Web.Config file for this script to work:

<identity impersonate="true"/>

Please note:
The “CreateDirectory” method of ASP.Net needs a special right. This right is only available for the IUSR account. To make sure that your request is handled by the IUSR, you have to place the above line in Web.Config.

This is my code in my registration page

    'Submitted
If Trim(Request("btnSubmit")) = "Submit" Then
  'validate-account
  _pgTest = Me.Master.MstrHsaMan.IsValidUserRecord("New", 0, 1, Request("txtUserName"), Request("txtEmail"), Request("txtPassword"), Request("txtVerify"), clsDrz_HSA_Declarations.UserStatusCode.UserPending, Me.Master.MstrError)
  If _pgTest Then
    'validate-company
    _pgTest = Me.Master.MstrHsaMan.IsValidCompanyRecord("New", 0, 0, Request("txtCompany"), Request("txtContactee"), Request("txtPhone"), Request("txtFax"), Request("txtMobile"), Request("txtCEmail"), Request("txtWebUrl"), clsDrz_HSA_Declarations.UserStatusCode.UserPending, Me.Master.MstrError)
    If _pgTest Then
      'save record
      _pgTest = Me.Master.MstrHsaMan.SaveNewCompanyRecord(Request("txtCompany"), Request("txtContactee"), Request("txtPhone"), Request("txtFax"), Request("txtMobile"), Request("txtCEmail"), Request("txtWebUrl"), _pgCompany, Me.Master.MstrError)
      If _pgTest Then
        'generate new company folder
        _pgTest = Me.Master.MstrHsaMan.SpawnCompanyFolder(Server.MapPath("System"), _pgCompany.CompanyId, Me.Master.MstrError)
        If _pgTest Then
          'generate new slides folder
          _pgTest = Me.Master.MstrHsaMan.SpawnSlidesFolder(Server.MapPath("System/Company" & _pgCompany.CompanyId), Me.Master.MstrError)
          If _pgTest Then
            'generate new videos folder
            _pgTest = Me.Master.MstrHsaMan.SpawnVideosFolder(Server.MapPath("System/Company" & _pgCompany.CompanyId), Me.Master.MstrError)
            If _pgTest Then
              'save user account record                  
              _pgTest = Me.Master.MstrHsaMan.SaveNewUserRecord(_pgCompany.CompanyId, Request("txtUserName"), Request("txtEmail"), Request("txtPassword"), _pgUser, Me.Master.MstrError)
              If _pgTest Then
                'SUCCESS saved
                Me.Master.MstrHsaMan.UpdateSuccessObjectParameters("Save New Company", "Company", Request("txtCompany"), "Contactee", Request("txtContactee"), "Telephone", Request("txtPhone"), "Fax", Request("txtFax"), "Mobile", Request("txtMobile"), Me.Master.MstrSuccess)
                HSAds_SuccessCtrl1.CtrlSuccessInfo = Me.Master.MstrSuccess
              Else
                'error detected
                Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Account", "SaveNewUserRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
                HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
              End If
            Else
              'error detected
              Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SpawnVideosFolder", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
              HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
            End If
          Else
            'error detected
            Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SpawnSlidesFolder", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
            HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
          End If
        Else
          'error detected
          Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SpawnCompanyFolder", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
          HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
        End If
      Else
        'error detected
        Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SaveNewCompanyRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
        HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
      End If
    Else
      'error detected
      Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Validate New Company", "IsValidCompanyRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
      HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
    End If
  Else
    'error detected
    Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Validate New Account", "IsValidAccountRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
    HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
  End If
End If
'

These are the routines I use to control the generation of the new folders on the remote server…

  '--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] SpawnCompanyFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Function SpawnCompanyFolder(ByVal pSvrRootPath As String, ByVal pCompanyId As Long, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxFolder As String = Nothing
'
'-> Initialise
SpawnCompanyFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
'-> Process Request
If Trim(pSvrRootPath) <> "" Then
  If pCompanyId > 0 Then
    sxFolder = "Company" & pCompanyId
    If fnCreateFolder(sxFolder, pSvrRootPath, pRtnErrObj) Then
      SpawnCompanyFolder = True
    Else
      'Create company folder error
      Select Case pRtnErrObj.ErrId
        Case clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
          pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_FAILEDTOCREATEFOLDER
          pRtnErrObj.ErrDescription = "The company folder (" & sxFolder & ") could not be created."
        Case clsDrz_HSA_Errors.eSYS_FOLDERALREADYEXISTS
          pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_FOLDERALREADYEXISTS
          pRtnErrObj.ErrDescription = "The company folder (" & sxFolder & ") could not be created."
        Case clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
          pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_FOLDERNAMEMISSING
          pRtnErrObj.ErrDescription = "The company folder name was found to be missing."
        Case Else
          pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_UNKNOWNDIRECTORYERROR
          pRtnErrObj.ErrTitle = "Unforseen File System Error!"
          pRtnErrObj.ErrDescription = "An unforseen file system access error has occurred."
          pRtnErrObj.ErrExternal = "It is recommended to restart your browsing session."
      End Select
    End If
  Else
    pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_COMPANYFOLDERIDINVALID
    pRtnErrObj.ErrTitle = "Invalid Folder Company Id!"
    pRtnErrObj.ErrDescription = "An invalid folder company-id parameter was passed into a function call."
    pRtnErrObj.ErrExternal = ""
  End If
Else
  pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_SERVERPATHMISSING
  pRtnErrObj.ErrTitle = "Server Path Not Specified!"
  pRtnErrObj.ErrDescription = "The server mapped path was not specified or was found to be missing."
  pRtnErrObj.ErrExternal = ""
End If
sxFolder = Nothing
'
End Function
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] SpawnSlidesFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Function SpawnSlidesFolder(ByVal pSvrRootPath As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxFolder As String = Nothing
'
'-> Initialise
SpawnSlidesFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
'-> Process Request
If Trim(pSvrRootPath) <> "" Then
  sxFolder = "Slides"
  If fnCreateFolder(sxFolder, pSvrRootPath, pRtnErrObj) Then
    SpawnSlidesFolder = True
  Else
    'Create company folder error
    Select Case pRtnErrObj.ErrId
      Case clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_FAILEDTOCREATEFOLDER
        pRtnErrObj.ErrDescription = "The company slides folder (" & sxFolder & ") could not be created."
      Case clsDrz_HSA_Errors.eSYS_FOLDERALREADYEXISTS
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_FOLDERALREADYEXISTS
        pRtnErrObj.ErrDescription = "The company slides folder (" & sxFolder & ") could not be created."
      Case clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_FOLDERNAMEMISSING
        pRtnErrObj.ErrDescription = "The company slides folder name was found to be missing."
      Case Else
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_UNKNOWNDIRECTORYERROR
        pRtnErrObj.ErrTitle = "Unforseen File System Error!"
        pRtnErrObj.ErrDescription = "An unforseen file system access error has occurred."
        pRtnErrObj.ErrExternal = "It is recommended to restart your browsing session."
    End Select
  End If
Else
  pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_SERVERPATHMISSING
  pRtnErrObj.ErrTitle = "Server Path Not Specified!"
  pRtnErrObj.ErrDescription = "The server mapped path was not specified or was found to be missing."
  pRtnErrObj.ErrExternal = ""
End If
sxFolder = Nothing
'
End Function
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] SpawnVideosFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Function SpawnVideosFolder(ByVal pSvrRootPath As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxFolder As String = Nothing
'
'-> Initialise
SpawnVideosFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
'-> Process Request
If Trim(pSvrRootPath) <> "" Then
  sxFolder = "Videos"
  If fnCreateFolder(sxFolder, pSvrRootPath, pRtnErrObj) Then
    SpawnVideosFolder = True
  Else
    'Create company folder error
    Select Case pRtnErrObj.ErrId
      Case clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_FAILEDTOCREATEFOLDER
        pRtnErrObj.ErrDescription = "The company videos folder (" & sxFolder & ") could not be created."
      Case clsDrz_HSA_Errors.eSYS_FOLDERALREADYEXISTS
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_FOLDERALREADYEXISTS
        pRtnErrObj.ErrDescription = "The company videos folder (" & sxFolder & ") could not be created."
      Case clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_FOLDERNAMEMISSING
        pRtnErrObj.ErrDescription = "The company videos folder name was found to be missing."
      Case Else
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_UNKNOWNDIRECTORYERROR
        pRtnErrObj.ErrTitle = "Unforseen File System Error!"
        pRtnErrObj.ErrDescription = "An unforseen file system access error has occurred."
        pRtnErrObj.ErrExternal = "It is recommended to restart your browsing session."
    End Select
  End If
Else
  pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_SERVERPATHMISSING
  pRtnErrObj.ErrTitle = "Server Path Not Specified!"
  pRtnErrObj.ErrDescription = "The server mapped path was not specified or was found to be missing."
  pRtnErrObj.ErrExternal = ""
End If
sxFolder = Nothing
'
End Function

The actual functions which create the folders are as follows…

  '--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] fnFolderExists
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Function fnFolderExists(ByVal pFldrPath As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxTest As Boolean = Nothing
Dim sxDirectory As System.IO.DirectoryInfo = Nothing
'
fnFolderExists = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
If Trim(pFldrPath) <> "" Then
  Try
    sxDirectory = New System.IO.DirectoryInfo(pFldrPath)
    sxTest = sxDirectory.Exists()
    If sxTest Then
      pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqNOERRORSFOUND
      pRtnErrObj.ErrTitle = ""
      pRtnErrObj.ErrDescription = ""
      pRtnErrObj.ErrExternal = ""
      fnFolderExists = True
    Else
      pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FOLDERDOESNOTEXIST
      pRtnErrObj.ErrTitle = "Specified Folder Not Found!"
      pRtnErrObj.ErrDescription = "The folder specified (" & pFldrPath & ") could not be found at the path specified."
      pRtnErrObj.ErrExternal = ""
    End If
  Catch ex As Exception
    pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FOLDERDOESNOTEXIST
    pRtnErrObj.ErrTitle = "Specified Folder Not Found!"
    pRtnErrObj.ErrDescription = "The folder specified (" & pFldrPath & ") could not be found at the path specified."
    pRtnErrObj.ErrExternal = ""
  End Try
Else
  fnFolderExists = clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
  pRtnErrObj.ErrTitle = "Folder Name Missing!"
  pRtnErrObj.ErrDescription = "No folder name/path specified."
  pRtnErrObj.ErrExternal = ""
End If
sxTest = Nothing
sxDirectory = Nothing
'
End Function
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] fnCreateFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Function fnCreateFolder(ByVal pNewFolder As String, ByVal pInFolder As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
fnCreateFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
If Trim(pInFolder) <> "" Then
  If Trim(pNewFolder) <> "" Then
    Try
      ' Determine whether the SOURCE directory exists. 
      If fnFolderExists(pInFolder, pRtnErrObj) Then
        If fnFolderExists(pNewFolder, pRtnErrObj) Then
          pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_NEWFOLDERALREADYEXISTS
          pRtnErrObj.ErrTitle = "Folder Exists Already!"
          pRtnErrObj.ErrDescription = "The new folder could not be created."
          pRtnErrObj.ErrExternal = "The folder (" & pNewFolder & ") could not be created because it exists already."
        Else
          Try
            System.IO.Directory.SetCurrentDirectory(pInFolder)
            Dim sxNewDir As System.IO.DirectoryInfo = System.IO.Directory.CreateDirectory(pNewFolder)
            pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqNOERRORSFOUND
            pRtnErrObj.ErrTitle = ""
            pRtnErrObj.ErrDescription = ""
            pRtnErrObj.ErrExternal = ""
            fnCreateFolder = True
          Catch ex As Exception
            pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
            pRtnErrObj.ErrTitle = "Failed To Create Folder!"
            pRtnErrObj.ErrDescription = "The folder (" & pNewFolder & ") could not be created."
            pRtnErrObj.ErrExternal = ex.Message
          End Try
        End If
      Else
        pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_INFOLDERMISSING
        pRtnErrObj.ErrTitle = "Source Folder Missing!"
        pRtnErrObj.ErrDescription = "The source folder was found to be missing."
        pRtnErrObj.ErrExternal = "The new folder could not be created because the source folder does not exist."
      End If
    Catch ex As Exception
      pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
      pRtnErrObj.ErrTitle = "Failed To Create Folder!"
      pRtnErrObj.ErrDescription = "The folder (" & pNewFolder & ") could not be created."
      pRtnErrObj.ErrExternal = ex.Message
    End Try
  Else
    pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_NEWFOLDERNAMEMISSING
    pRtnErrObj.ErrTitle = "New Folder Name Missing!"
    pRtnErrObj.ErrDescription = "The new folder name was found to be missing."
    pRtnErrObj.ErrExternal = "The new folder could not be created because no name was specified."
  End If
Else
  pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_INFOLDERNAMEMISSING
  pRtnErrObj.ErrTitle = "Source Folder Name Missing!"
  pRtnErrObj.ErrDescription = "The source folder name was found to be missing."
  pRtnErrObj.ErrExternal = "The new folder could not be created because no source folder name was specified."
End If
'
End Function

As far as I can see, all the code is correct and error free, and runs seamlessly on my development server….

I feel it is a configuration issue possibly in the web.config file and I am stuck now and not getting much help from support, the support department advises me to check my code!

Please can someone look at my code above and the config file below and point me in the right direction…

Much Thanks

Platform is .NET 4.0/4.5
Hosting is shared Windows an runs on Medium trust.

I am not sure if my application is trying to something in full trust(?)

My Data folder rights Allow (Modify/Read & Execute/Read/Write)
My Data folder rights Deny (nothing is denied)

My Data Folder parent folder rights Allow (Modify/Read & Execute/Read/Write)
My Data Folder parent folder rights Deny (nothing is denied)

My web.config file

<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
        <!-- identity impersonate="true"/ -->
        <!-- 
        Line above commented out as it causes the site to crash
        without loading any pages and the error points to this line 
        -->
    </system.web>
</configuration>

The Error message ….

enter image description here


Source: New Questions

Leave a Reply