locked
File not saving after Impersonation. RRS feed

  • Question

  • User1655374113 posted

    Hello,

    I am saving the uploaded file in the below location. 

    AttachmentName =  Guid.NewGuid.ToString() 
    SavePath = "Z:\NAUUploadedFiles\" + AttachmentName
    fuAttachments.SaveAs(SavePath)

    After publishing my code to server, Files are not uploading for other users as they permission issues. So, I implement impersonation by following the below link

    http://support.microsoft.com/kb/306158#2

    I impletemented "Impersonate a Specific User in Code" and give my domain, username and password. Now the code looks like this. 

     Try
         If objSecurityService.ImpersonateUser(strReportsUser, strReportsUserDomain, strReportsPassword) Then
             bImpersonate = True
              
              Dim windowsIdentity As WindowsIdentity = DirectCast(System.Security.Principal.WindowsIdentity.GetCurrent(), WindowsIdentity)
    
              AttachmentName = Guid.NewGuid.ToString()
              SavePath = "Z:\NAUUploadedFiles\" + AttachmentName
              fuAttachments.SaveAs(SavePath)
           End If
          Finally
              If bImpersonate Then
                  objSecurityService.UnImpersonate()
              End If
          End Try

    Now, it is throwing "DirectoryNotFoundException" error at savepath 

    Monday, June 23, 2014 10:09 AM

Answers

All replies

  • User-760709272 posted

    is "Z" a mapped drive?  If so you should try the raw UNC instead;

    \\yourserver\yourshare\NAUUploadedFiles\

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 23, 2014 10:20 AM
  • User1655374113 posted

    is "Z" a mapped drive?  If so you should try the raw UNC instead;

    \\yourserver\yourshare\NAUUploadedFiles\

    Hi,

    Yes, its a mapped drive. I tried with servername also.

    What is yourshare in the above one ?

    Monday, June 23, 2014 10:28 AM
  • User753101303 posted

    Hi,

    This is where Z: points. the point is that mapped drives depends on the user profile. So for server apps it's better to directly use the network share you want rather than to use a mapped drive (which more likely is not mapped for the account under which your app runs).

    Impersonation is not needed if you give access to the account under which your app runs.

    Monday, June 23, 2014 10:31 AM
  • User1655374113 posted

    This is where Z: points. the point is that

    So, in place of yourshare, you want me to give Z: ?

    Monday, June 23, 2014 10:35 AM
  • User-760709272 posted

    You'll probably need to ask your system adminstrator what the UNC path to your folder is, or google how you work it out from the mapped drive, there is probably a way, eg going to the "Mapped drives" section in windows explorer.

    Monday, June 23, 2014 10:39 AM
  • User753101303 posted

    What I meant is that when you want to create a network share on a server you give a "share name" which is usually by default the name of the folder you are sharing.

    So if your share somefolder you'll access this by using \\servername\yourfolder. Then you can map a drive letter to this location so that users are using a drive letter. For programmatic access using directly \\servername\yourfolder is much better as the mapping is a profile specific configuration and Z: could even point to a totally different location etc...

    Try :

    net use

    on the command line to see where your Z: drive is pointing to.

    Monday, June 23, 2014 11:26 AM