Accessing virtual directory(mapped drive) via c#/asp.net webpage with IIS7

13,061

Solution 1

As wata suggests, each user gets their own mapped drives. This means that your L: drive is not the same as your app pool account's L: drive.

In addition, unless you've changed the account your app pool is running as, it won't be able to access the shared folder on the other server. Most likely you're logging in to both servers using an Active Directory domain account. If so, you will probably want to create a new Active Directory domain account to use as the identity for your app pool. You could change the app pool identity to use your own domain account for dev/testing purposes, but that's not a recommended security practice in a production system.

Once you've created the new Active Directory "service account" (to avoid future hassle, make sure the password doesn't expire), you'll want to change your app pool's identity in IIS. Go to Application Pools, find the app pool in use by your site, select it and choose Advanced Settings on the right, go to Identity, and click the ... button to set the custom account, making sure to prefix the username with the domain name: mydomain\myserviceusername.

You'll also want to grant your service account access to Server B's share.

Now you will need to create a persistent mapped drive from Server A to Server B using your service account. See this for details, making sure to set up a script that remaps the drive after a reboot using a command such as net use L: \\ServerB\sharedfolder /persistent:yes, making sure this is run as your service account. You could potentially even run this first thing in your app's Global.asax.cs Application_Start. If you want to avoid the hassle of the steps in this paragraph, use wata's suggestion of using the full UNC path instead of using a mapped drive.

Now your web app should be able to access the shared folder on Server B. :-)

Solution 2

The problem is that if you create a mapped drive it is only visible to the user who created it. Since your application's IIS Application Pool probably runs under a different user, this mapped drive is invisible to it. (Running the app under a different user account is a good security practice)

I suggest the following: instead of using a Mapped Drive for the virtual directory, try to use an UNC path. For example: create a virtual directory called "documents" which maps to \\ServerB\a\b. Then you should be able to access your file with "documents/file.pdf". Keep in mind that the application's IIS Application Pool user needs to have access to the network share \\ServerB\ and the subfolders and files you need.

Solution 3

Check the permissions of the folder on the server and make sure the application pool of the site has access to it.

Share:
13,061
Mana
Author by

Mana

Software Developer

Updated on June 05, 2022

Comments

  • Mana
    Mana about 2 years

    So i have a server A and server B.

    Server A: Windows Server 2008R2 Server B: Windows Server 2003

    Web page is using framework 4.0, created with VS2013 Pro RC

    on server A my asp.net/c# webpage is running on IIS7 on server B i have a shared folder.

    Now i have mapped this shared folder from server B to server A, and its fully accessible via the Desktop\Windows Explorer, however accessing the folder from the webpage is a different story.

    To access the folder, what i have done in IIS7 is, create a virtual folder under the same webpage, and point it to the mapped drive.

    This would of course have worked if the folder would have been on the same server A, but since it's on a different server B, i get the following error.

    Could not find a part of the path 'L:\a\b\file.pdf' now the path is 100% correct, since i have checked.


    Heres some additional debug info:

    Could not find a part of the path 'L:\a\b\file.pdf'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'L:\a\b\file.pdf'.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [DirectoryNotFoundException: Could not find a part of the path 'L:\a\b\file.pdf'.] System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +216 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +2481 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +229
    System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +102
    System.Web.HttpResponse.WriteFile(String filename, Boolean readIntoMemory) +166 Reloc.Client.Contracts.openLinkClick(Object sender, EventArgs e) in c:\Users\x\Documents\Visual Studio 2013\Projects\p\p\S\Listdoc.aspx.cs:230
    System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1192
    System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +164 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +52
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707


    I beleive this might have something to do with permission or related, have tried numerous thing, without luck. So please help me out here. Thanks in advance.

  • Mana
    Mana over 10 years
    Can you tell me how i can give permission to application pool of the site, or refer me somehere i can read about it?
  • Mana
    Mana over 10 years
    are you talking about the permissions on the shared folder? and must i change the permissions on server A or B
  • Mana
    Mana over 10 years
    Thanks for the detailed explanation, that clarified allot, and i think we have it soon working, just waiting for some permissions from admin.
  • Tony L.
    Tony L. almost 9 years
    For those that need to find the UNC (Universal Naming Convention) of a mapped drive, type net use into the command prompt per stackoverflow.com/questions/21482825/…