Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Create Directory

    Hello,

    I am having a permissions problem when creating a directory. The relevant
    bits of my code look like this:

    // impersonate current user:
    WindowsIdentity ident = (WindowsIdentity)
    HttpContext.Current.User.Identity;
    _context = ident.Impersonate();
    _name = ident.Name;
    _isauth = ident.IsAuthenticated;
    _authtype = ident.AuthenticationType;

    // create the directory
    // I've also tried using DirectoryInfo.Create()
    Directory.CreateDirectory(directoryName);

    // restore current (aspnet) identity:
    _context.Undo();

    If "directoryname" is "e:\inetpub\wwwroot\unittests\createdirectory\ New" I
    am getting the error Could not find a part of the path "e:\".

    If I grant this user access to the root of my "e" drive then it works
    fine. However, doing this is a security risk (and also, to me, seems
    completely unnecessary).

    Does anyone know why this happens and if there is a sensible way round
    it? Is this a .net bug?

    Any help would be very much appreciated as I am completely stumped!

    Thanks,

    John
    John Guest

  2. Similar Questions and Discussions

    1. Cannot create directory
      Hey, I am trying to install contribute cs3 for a client. I am having trouble setting up the connection to their company website. Once I put...
    2. change into a directory to create other folders/directory
      I need to create sub folders in a parent folder.. i have ... <cfset session.parentdir = "e:\mainstuff\"> <cfdirectory action="create"...
    3. How would I create a directory in ASP.NET?
      I've got a permission issue when I try to create a folder programmatically using C# in an ASP.NET web service. I've set ASP.NET user to have full...
    4. can't create directory from web service
      Using the Directory object I issue a createdirectory command and get an exception: System.IO.DirectoryNotFoundException: Could not find a part of...
    5. How to create a new Virtual Directory?
      I have bought an asp.net host,but there is only one Virtual Directory. How to create a new Virtual Directory in an ASP.NET application? Thanks!
  3. #2

    Default Re: Create Directory

    > I am having a permissions problem when creating a directory. The
    > relevant bits of my code look like this:
    >
    > <<SNIPPED>>
    >
    > If "directoryname" is "e:\inetpub\wwwroot\unittests\createdirectory\ New"
    > I am getting the error Could not find a part of the path "e:\".
    >
    > If I grant this user access to the root of my "e" drive then it works
    > fine. However, doing this is a security risk (and also, to me, seems
    > completely unnecessary).
    >
    > Does anyone know why this happens and if there is a sensible way round
    > it? Is this a .net bug?
    Is the only way round this really to grant read directory permissions for
    the user on the root of the drive?

    This seems very odd to me. Has no one else come across this before?

    Thanks,

    John
    John Guest

  4. #3

    Default Re: Create Directory

    This is a relatively known .NET bug or "feature"...



    Both Directory.CreateDirectory(path) and
    DirectoryInfo.CreateSubdirectory(path) require user to have Read access to
    the drive's root directory (i.e. <Drive>:\).



    Many ASP.NET hosting providers (especially those running Windows 2003
    Server) will not allow user running ASP.NET worker process read access to
    the root folder, so CreateDirectory will always fail. You can not blame
    hosting providers - they do right thing, securing shared environment from
    users with malicious intents.



    The only workaround I have found is to replace call to
    Directory.CreateDirectory() with call to unmanaged code, like msvcrt's
    _mkdir(char*):



    [DllImport("msvcrt.dll", SetLastError=true)]

    static extern int _mkdir(string path);



    ....

    //replace call to Directory.CreateDirectory with:

    _mkdir(newDirectory);

    ....



    This will work only if your code is granted "Allow Calls to Unmanaged Code"
    permission but most hosting environments allow that.



    You can find more details in my recent Blog entry at
    [url]http://hatka.net/wlogdev/archive/2004/08/29/178.aspx[/url]



    Dmitry Kulakovsky



    "John" <nospam@please.com> wrote in message
    news:opr6uojhcr22ma4c@cl213.castletown.pdms.com...
    > Hello,
    >
    > I am having a permissions problem when creating a directory. The relevant
    > bits of my code look like this:
    >
    > // impersonate current user:
    > WindowsIdentity ident = (WindowsIdentity)
    > HttpContext.Current.User.Identity;
    > _context = ident.Impersonate();
    > _name = ident.Name;
    > _isauth = ident.IsAuthenticated;
    > _authtype = ident.AuthenticationType;
    >
    > // create the directory
    > // I've also tried using DirectoryInfo.Create()
    > Directory.CreateDirectory(directoryName);
    >
    > // restore current (aspnet) identity:
    > _context.Undo();
    >
    > If "directoryname" is "e:\inetpub\wwwroot\unittests\createdirectory\ New" I
    > am getting the error Could not find a part of the path "e:\".
    >
    > If I grant this user access to the root of my "e" drive then it works
    > fine. However, doing this is a security risk (and also, to me, seems
    > completely unnecessary).
    >
    > Does anyone know why this happens and if there is a sensible way round
    > it? Is this a .net bug?
    >
    > Any help would be very much appreciated as I am completely stumped!
    >
    > Thanks,
    >
    > John

    Dmitry Kulakovsky Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139