1 year ago

#375560

test-img

anne

How to Impersonate when download File from Share Folder?

I want to impersonate when get files from Shared Folder and download the files. I referred to this link, https://learn.microsoft.com/en-GB/troubleshoot/developer/webapps/aspnet/development/implement-impersonation#impersonate-a-specific-user-in-code

I am able to impersonate when get files from Shared Folder. However, got error Access Denied, when Download the file.

ImpersonateValidUser("username", "domain", "password");

DirectoryInfo dir = new DirectoryInfo("path");
if (dir.Exists)
{
   FileInfo[] files = dir.GetFiles();
   if (files != null)
   {
      FileInfo selectedFI = files.FirstOrDefault();
      if (selectedFI != null)
      {
        string ext = Path.GetExtension(selectedFI.FullName);
        Response.ClearHeaders();
        if (type != null)
           Response.ContentType = type;

        Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(selectedFI.FullName));

        if (ext == ".pdf")
        {
           Response.TransmitFile(selectedFI.FullName);
           Response.End();
        }
        else
        {
           Response.AppendHeader("Content-Length", selectedFI.Length.ToString());
           Response.WriteFile(selectedFI.FullName);
        }
      }
   }
}

UndoImpersonation();

c#

asp.net

impersonation

0 Answers

Your Answer

Accepted video resources