简体   繁体   中英

Allow Powershell script download from webpage in IIS

I am trying to update a Razor page (running on IIS 10.0 version 1809) to allow me to share a Powershell script. I want the user to be able to right-click the link and select "save" to download the script from a folder on the web server (wwwroot/<site name>/downloadFiles). I verified that the NTFS permissions allow "Read & Execute", "List folder contents", and "Read" to IUSR, Network Service, Users, and IIS_IUSRS.

I tested:

  • Right-click the link and select "Save link as..." and Chrome tells me, "Failed - No file"
  • Browse to https://<site url>/<site name>/downloadFiles/script.ps1 and IIS tells me: 404 - File or directory not found. The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
  • Browse to https://<site url>/<site name>/downloadFiles and IIS tells me: 403 - Forbidden: Access is denied. You do not have permission to view this directory or page using the credentials that you supplied.

In _Layout.cshtml, I have the link setup as:

<a href="~/downloadFiles/script.ps1">Script</a>

Any idea how I can make this work?

Use the FileExtensionContentTypeProvider to map the .ps1 extension to the application/octet-stream mime type to force a download as explained in the docs: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files#fileextensioncontenttypeprovider

var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".ps1"] = "application/octet-stream";
app.UseStaticFiles(new StaticFileOptions
{
    ContentTypeProvider = provider
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM