How do I serve static files only to authorized users?

前端 未结 5 862
梦毁少年i
梦毁少年i 2020-12-02 22:51

I have a collection of Excel spreadsheets that I\'d like to serve in my ASP.NET 5 webapp only to authorized users.

  1. Where should I store the files? I assume in
5条回答
  •  抹茶落季
    2020-12-02 23:22

    in .net core create a dedicated directory www in same level as wwwroot, and use the following code:

    public HomeController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }
    
    [Authorize(Roles = "SomeRole")]
    public IActionResult Performance()
    {
        return PhysicalFile(Path.Combine(_hostingEnvironment.ContentRootPath,
                                         "www", "MyStaticFile.pdf"), "application/pdf");
    }
    

    Based on the following answer (for .netCore): static file authorization

提交回复
热议问题