-
Notifications
You must be signed in to change notification settings - Fork 598
Cookie auth always returns 302 Found #588
Comments
@HaoK can you try to repro this? |
I've no events, some calls return me 200 (how should be), others return me 302 Found. |
I've identified the root cause for my problem: aspnet/KestrelHttpServer#365 |
I cannot repro this on current bits. If you can repro this with RC2 bits, please reopen the issue... I tried:
And I was able to verify that the response code was 405 MethodNotAllowed so its getting called |
@HaoK I'll try again when the RC2 bits are out and let you know. |
Today I understood (by accident) what is actually happening. My code looks like this, any idea? app.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieName = "Test",
SessionStore = new MemoryCacheTicketStore(),
Events = new CookieAuthenticationEvents
{
// TODO: not working in RC1?
OnRedirectToAccessDenied = context => { context.Response.StatusCode = 403; return Task.CompletedTask; },
OnRedirectToLogin = context => { context.Response.StatusCode = 401; return Task.CompletedTask; },
}
}); |
OK some final feedback on this ticket. So the actual problem was that I'm using ASP.NET Identity. When I thought that I was configuring my cookies, it did not have any effect because the cookie middleware used is in fact the one linked to the application auth. scheme, as defined by Identity. Some feedback on all this, though. The configuration of this stuff is rather confusing and being wrong is totally silent. Some config APIs are really not consistent, not intuitive and easy to get wrong. Case in point: (1) most middleware accept an option instance, but (2) you will be tempted (I was) to do services.AddIdentity(options =>
{
options.Lockout = new LockoutOptions() { ... }; // Initializer seems fine.
var appCookie = options.Cookies.ApplicationCookie; // Initializer is not fine here
appCookie.CookieName = "MyApp.Auth"; Then comes appCookie.Events = new CookieAuthenticationEvents
{
OnRedirectToAccessDenied = context => { context.Response.StatusCode = 403; return Task.CompletedTask; And now you did a mistake again! Because Identity has a default events to validate the security stamp. So if you want to faithfully enhance the default options, you have to know that you should add the following line to your config: OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
}
}); Honestly, without digging deep into the sources, there is no way you can come up with the correct configuration on your own (by correct I mean: take the default config and tweak just one or two options to fit your needs). This code is not intuitive at all, is ugly and most "mistakes" are silent. |
Sounds like at a minimum we need to remove the setter on options.Cookies.ApplicationCookie. @HaoK |
@Tratcher log a new bug for this? |
I'm not sure removing the setter is the right thing. I do agree this is pretty complex but basically it comes down to two different scenarios for when to tweak vs replace via the setter.
AddIdentity is also a bit weird since unlike UseCookie and the other middleware, the cookie options are configured via identity options because UseIdentity is what eventually calls UseCookie(options.Cookies.Application) |
@HaoK not sure what the right thing is, but this is too complex to use. Given it's security and may silently not do what you expect this is bad. Modifying the instance will surely get a lot easier if C# 7 gets Setting a new instance "feels" right and since I modified 6 properties, I thought it may qualify as "lots" as you said. The problem is that when you do The other trouble point is |
At this point I recommend taking this discussion to aspnet/Identity#744 |
Yeah lets take the identity specific stuff to that repo. In regards to the Events pattern, I personally do not like that at all either, but I lost that battle... I would have preferred having no interface and a very simple Events class with a get/set delegate property for each event |
I upgraded from beta-7 to rc1.
Following the work done in issue #458 I rewrote my code like this:
But when sending an HTTP request without the auth cookie, I receive a 302 found that has Location set to login page rather that 401.
Putting a breakpoint in my delegate, it seems it is not called at all (makes sense then...).
What am I doing wrong?
The text was updated successfully, but these errors were encountered: