I am trying to use Cookie authentication in my .net core MVC project. The process is that After Registration I have an entry in the ASPNetUsers table and I have extended this table to include a userType. After the user has logged in I can see that there is a cookie in the cookies collection through the chrome browser. When I try to redirect the user after login it fails. If I adorn the method I'm redirecting with
[AllowAnonymous]the method gets hit, so I know the redirect is correct.
At the top of my controller I have
[Authorize(ActiveAuthenticationSchemes = "Cookie")]
When I start the application I register Identity with the following bit of code:
services.AddIdentity<ApplicationUser, IdentityRole<int>>( config => { config.User.RequireUniqueEmail = true; config.Cookies.ApplicationCookie.LoginPath = "/Account/Login"; config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() { OnRedirectToLogin = async ctx => { if (ctx.Request.Path.StartsWithSegments("/visualjobs") && ctx.Response.StatusCode == 200) { ctx.Response.StatusCode = 401; } else { ctx.Response.Redirect(ctx.RedirectUri); } await Task.Yield(); } }; }).AddEntityFrameworkStores<VisualJobsDbContext, int>() .AddDefaultTokenProviders();
and in the configuration of start up I have:
app.UseIdentity(); app.UseCookieAuthentication(new CookieAuthenticationOptions() { AuthenticationScheme = "Cookie", // LoginPath = new PathString("/Account/Login/"), AutomaticAuthenticate = false });
What have I missed?