Skip to content
Loading
  • Sachin Singh
    +2
    @varun, it is a good solution , but if someone type the Url :- domain/folder/yourdoc.doc or domain/folder/yrimg.png then , he can access it , isn't it?
    +2
  • Dhruval Dave
    +1
    Hi There,
     
    If possible, Take a look at https://docs.microsoft.com/en-us/iis/manage/configuring-security/understanding-iis-url-authorization
     
    Do letus know if you need anymore guidelines 
    +1
  • Sachin Singh
    +1
    Thanks for reply everyone,
    I do not have access to IIS @Dhruval,
    How to secure a static folder or file in MVC not in core @varun.
    +1
  • Varun Setia
    0
    Hi,
     
    In .NET Core, we can do something like this if we want to use proper authorization,
     
    1. [Authorize]  
    2. public IActionResult Doc(name)  
    3. {  
    4.     var filePath = Path.Combine(  
    5.         _env.ContentRootPath, "myFolder", name);  
    6.   
    7.     return PhysicalFile(filePath, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");  
    8. }  
     Else,
     
    You if you want to view document in wwwroot by anyone use the code below and place file anywhere inside wwwroot,
     
    1. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
      //... 
    2.    app.UseStaticFiles();
    3. //..  
     
    0
  • Varun Setia
    0
    Yes, that is Pre-Requisite. We must place files in folder that is not publicallly accessible. It can be ../../myFolder or drive://myFolder
    0