Reputation: 3117
We have a Login enforcement app which only allows a user to be logged into a single session in our app. This is done in the postauthenticate request event handler.
If the user needs to be logged out (they have another session) we do:
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
This has worked fine for a long time. Now we are adding a web api to our app. When you hit a web api Url the PostAuthentication event does fire, it sees the user has logged in elsewhere... however, it does a redirect to login.. well that's expected due to the code.
I attempted to change the code above to set the Response.StatusCode = 401
and then end the request. Well, that works fine, but asp.net is being "nice" and auto redirecting to the login page.
Is there a way I can respond with a 401 and not have it redirect to the login page if this request came from web api URL?
Upvotes: 1
Views: 1088
Reputation: 47
You can set SuppressFormsAuthenticationRedirect of the HttpResponse to true and the FormsAuthentication module will not catch the 401.
Upvotes: 1
Reputation: 23894
The problem is that the FormsAuthenticationModule catches all outbound requests at EndRequest and if the status is 401 AND the URL isn't the configured login location, you get redirected to the login location.
There's not any great way to fix that. Basically I've seen two ways, neither of which are awesome:
Upvotes: 1