-
Notifications
You must be signed in to change notification settings - Fork 598
Can't set Response.StatusCode to 401 in JwtBearerEvents #1154
Comments
@onedevteam can you show us the entire stack trace? This is definitely quite odd. It would be even more helpful if you can upload your app to GitHub so we can see what's going on. |
Can't post whole project. I'm following stormpath tutorial to implement jwt. After some research, i managed to make it work by returning Here's exception:
|
@onedevteam thanks for posting the stack trace. @Tratcher any ideas? |
Ok, that makes a bit more sense. OnAuthenticationFailed has executed and set a 401 (with a body) and returned. As the pipeline unwinds then the middleware calls
Bug: HandleAutomaticChallengeIfNeeded should check Response.HasStarted before calling HandleUnauthorizedAsync. It's your OnAuthenticationFailed event that sets the 401, correct? Workarounds: |
@Tratcher It's your OnAuthenticationFailed event that sets the 401, correct? Yes. I have checked it in step-by-step debugger, and on first pass, before i set it to 401, value of c.Response.StatusCode is 200. Then, on second pass, it's 401 (set manualy, in 1st pass), and in 3rd pass it throws an error... I also tested with JwtBearerOptions.AutomaticChallenge, off, and it passed. |
Hi, we just tried to analyse a similar issue today. |
Closing for the following reasons:
|
@Eilon |
@Zonciu we don't track closed issues, please open a new issue and describe your scenario. |
I have following code in my Startup.Auth.cs partial:
Goal is to intercept unauthorized requests and return 401 code instead of login form for API requests.
Problem is, when i set c.response.statuscode to 401, i get runtime exception
System.InvalidOperationException: 'StatusCode cannot be set, response has already started.'
Every other response works (200, 201, 202, 300, 400, 402, 403... etc.. tested with postman) except one i need.
The text was updated successfully, but these errors were encountered: