Redirect to login when unauthorized in ASP.NET Core

后端 未结 6 1286
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 07:10

    For anyone that's interested it can also be done with the AddIdentity service provider.

    services.AddIdentity(options =>
        {
            options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
            options.Cookies.ApplicationCookie.AutomaticChallenge = true;
            options.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
        })
        .AddEntityFrameworkStores()
        .AddDefaultTokenProviders();
    

    And as explained here: https://stackoverflow.com/a/41643105/5784635

    I attempted this in April 2017 and "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0" doesn't redirect I had to use the 1.0.1 version

提交回复
热议问题