Security icon indicating copy to clipboard operation
Security copied to clipboard

Improve error message for when AuthenticationScheme is not recognized

Open XplMarcel opened this issue 11 years ago • 9 comments

If AutomaticAuthentication is disabled through: services.ConfigureCookieAuthentication(c => c.AutomaticAuthentication = false); requests to controllers with an Authorize attribute give a 500 Internal Server Error with the following error:

System.InvalidOperationException The following authentication scheme was not accepted:

There is no authentication scheme at all. It can be reproduced using the default asp.net 5 WebApplication template (with individual accounts). Just browse directly to http://localhost:{port}/Manage

Any ideas ?

XplMarcel avatar Sep 18 '15 13:09 XplMarcel

I have recognised this behaviour also.

dcarr42 avatar Sep 18 '15 13:09 dcarr42

By design. You must have an automatic auth middleware or your Authorize attribute must name specific auth middleware to use. Otherwise Authorize wouldn't have any effect.

Tratcher avatar Sep 18 '15 14:09 Tratcher

We could re-purpose this issue to be: experience (sucks)/errors are not clear when missing authentication handlers...

HaoK avatar Sep 18 '15 19:09 HaoK

I agree that automatic auth middleware should be in place, but in this case it was in place since i was using Identity, where app.UseIdentity() clearly adds CookieAuthentication. So personally i think the problem occurs in Identity if AutomaticAuthentication is disabled. I wanted to disable AutomaticAuthentication, because redirects are useless in a pure WebApi. It can easily be solved using CookieAuthenticationNotifications, but i think a 500 internal server error shouldn't happen in the first place.

XplMarcel avatar Sep 21 '15 08:09 XplMarcel

@HaoK is UseIdentity even relevant for a pure WebApi scenario?

Tratcher avatar Sep 21 '15 17:09 Tratcher

Not really, as all UseIdentity does is add a bunch of cookie middlewares. So if the cookies are not needed, best to just not call UseIdentity

HaoK avatar Sep 21 '15 18:09 HaoK

Though we're not planning to change the behavior, we will improve the error message experience.

@HaoK - you had mentioned there might be a dup in HttpAbstractions, so feel free to dup it against that.

Eilon avatar Sep 24 '15 22:09 Eilon

https://github.com/aspnet/HttpAbstractions/pull/421

HaoK avatar Sep 30 '15 23:09 HaoK

Duping against https://github.com/aspnet/Security/issues/511

HaoK avatar Oct 09 '15 20:10 HaoK