Redirect to login when unauthorized in ASP.NET Core

后端 未结 6 1287
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 06:24

In the previous ASP.NET MVC, there was an option to redirect to the login action, if the user was not authenticated.

I need the same thing with ASP.NET Core, so I:

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 07:23

    this code block in the startup file works for me in .Net Core 3.1

    services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
    
            options.LoginPath = "/Identity/Account/Login";
            options.AccessDeniedPath = "/Identity/Account/AccessDenied";
            options.SlidingExpiration = true;
        });
    
    

提交回复
热议问题