Migrating web api authentication from .NET Core 1.1 to 2.0 -
i'm trying convert old authentication .net 2.0. had following code:
app.usejwtbearerauthentication(new jwtbeareroptions { automaticauthenticate = true, includeerrordetails = true, authority = "https://securetoken.google.com/xxxxx", tokenvalidationparameters = new tokenvalidationparameters { validateissuer = true, validissuer = "https://securetoken.google.com/xxxxxx", validateaudience = true, validaudience = "xxxx", validatelifetime = true, }, });
my new code following:
public void configure(...) { ... app.useauthentication(); ... } public void configureservices(iservicecollection services) { ... services.addauthentication(jwtbearerdefaults.authenticationscheme) .addjwtbearer(options => { options.requirehttpsmetadata = false; options.includeerrordetails = true; options.authority = "https://securetoken.google.com/xxxxxx"; options.tokenvalidationparameters = new tokenvalidationparameters { validateissuer = true, validissuer = "https://securetoken.google.com/xxxxx", validateaudience = true, validaudience = "xxxxxx", validatelifetime = true, }; }); ... services.addmvc(); services.addauthorization(......); }
but in 2.0 i'm getting 404 response. if remove [authorize]
attribute endpoint, works. output window shows this:
microsoft.aspnetcore.hosting.internal.webhost:information: request starting http/1.1 http://localhost:62423/api/users/info
microsoft.aspnetcore.authorization.defaultauthorizationservice:information: authorization failed user: (null). microsoft.aspnetcore.mvc.internal.controlleractioninvoker:information: authorization failed request @ filter 'microsoft.aspnetcore.mvc.authorization.authorizefilter'. microsoft.aspnetcore.mvc.challengeresult:information: executing challengeresult authentication schemes ().microsoft.aspnetcore.authentication.cookies.cookieauthenticationhandler:information: authenticationscheme: identity.application challenged. microsoft.aspnetcore.mvc.internal.controlleractioninvoker:information: executed action sorte.api.contentmanager.controllers.userscontroller.info (sorte.api.contentmanager) in 24.0837ms microsoft.aspnetcore.hosting.internal.webhost:information: request finished in 35.2446ms 302 microsoft.aspnetcore.hosting.internal.webhost:information: request starting http/1.1 http://localhost:62423/account/login?returnurl=%2fapi%2fusers%2finfo
microsoft.aspnetcore.hosting.internal.webhost:information: request finished in 5.8149ms 404
from log errors, seems it's trying redirect me /account/login
, don't have such endpoint, project web api.
am missing configuration?
i facing same problem, until read this.
when use authorize attribute, binds first authentication system default.
the solution especify wich scheme use (jwtbearer):
[authorize(authenticationschemes = jwtbearerdefaults.authenticationscheme, policy = "policename")]
now can status 200 (with valid token) , 401 (unauthorized - invalid token)
Comments
Post a Comment