How do I serve static files only to authorized users?

前端 未结 5 861
梦毁少年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:21

    This is a very simple example, but it can be changed to check for specific roles, and the code can be moved out of the Startup.cs for more flexibility.

    app.Use(async (context, next) =>
                   {
                       if (!context.User.Identity.IsAuthenticated
                           && context.Request.Path.StartsWithSegments("/excelfiles"))
                       {
                           throw new Exception("Not authenticated");
                       }
                       await next.Invoke();
                   });
    

提交回复
热议问题