How do I serve static files only to authorized users?

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

    For authentication check while retrieving file:

            app.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = (context) =>
                {
                    if (!context.Context.User.Identity.IsAuthenticated && context.Context.Request.Path.StartsWithSegments("/excelfiles"))
                    {
                        throw new Exception("Not authenticated");
                    }
                }
            });
    

提交回复
热议问题