I had this problem in an Angular2 + ASP.NET Core application. I managed to fix it as follows:
services.AddIdentity<ApplicationUser, IdentityRole>(config => {
If this does not work for you, you can try with the following method:
services.AddIdentity<ApplicationUser, IdentityRole>(config => { // ... config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents { OnRedirectToLogin = ctx => { if (ctx.Request.Path.StartsWithSegments("/api")) { ctx.Response.StatusCode = (int) HttpStatusCode.Unauthorized; // added for .NET Core 1.0.1 and above (thanks to @Sean for the update) ctx.Response.WriteAsync("{\"error\": " + ctx.Response.StatusCode + "}"); } else { ctx.Response.Redirect(ctx.RedirectUri); } return Task.FromResult(0); } }; // ... }
Update for Asp.Net Core 2.0
Cookie settings are now configured as follows:
services.ConfigureApplicationCookie(config => { config.Events = new CookieAuthenticationEvents { OnRedirectToLogin = ctx => { if (ctx.Request.Path.StartsWithSegments("/api")) { ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; } else { ctx.Response.Redirect(ctx.RedirectUri); } return Task.FromResult(0); } }; });
Darkseal
source share