asp.net web api2 - "error": "invalid_client" from custom OWIN implementation -
i implementing owin authentication on mysql backend, dont thnk thats problem registration work pretty well. have worked off this post (i.e. nicked of code).
i using di via autofac have changed few things around inject dependencies simpleauthorizationserverprovider
the problem post grant_type=password, username , password http://localhost/myappurl/token
, "error":"invalid_client". no hits when seek debug failing in library , not getting own code. know why be?
please pardon lengthy code, have no thought issue have posted think relevant, if needs see more code, please ask.
simpleauthorizationserverprovider
public class simpleauthorizationserverprovider : oauthauthorizationserverprovider { private readonly iuserservice _userservice; public simpleauthorizationserverprovider(iuserservice userservice) { _userservice = userservice; } public override async task validateclientauthentication(oauthvalidateclientauthenticationcontext context) { context.validated(); } public override async task grantresourceownercredentials(oauthgrantresourceownercredentialscontext context) { context.owincontext.response.headers.add("access-control-allow-origin", new[] { "*" }); var authenticate = await _userservice.finduser(context.username, context.password); if (!authenticate) { context.seterror("invalid_grant", "the user name or password incorrect."); return; } var identity = new claimsidentity(context.options.authenticationtype); identity.addclaim(new claim("sub", context.username)); identity.addclaim(new claim("role", "user")); context.validated(identity); } }
startup
public partial class startup { public void configureauth(iappbuilder app) { httpconfiguration config = new httpconfiguration(); configureoauth(app, (ioauthauthorizationserverprovider)config.dependencyresolver.getservice(typeof(ioauthauthorizationserverprovider))); app.usecors(microsoft.owin.cors.corsoptions.allowall); app.usewebapi(config); } public void configureoauth(iappbuilder app, ioauthauthorizationserverprovider provider) { oauthauthorizationserveroptions oauthserveroptions = new oauthauthorizationserveroptions() { allowinsecurehttp = true, tokenendpointpath = new pathstring("/token"), accesstokenexpiretimespan = timespan.fromdays(90), provider = provider, applicationcandisplayerrors=true, }; app.useoauthauthorizationserver(oauthserveroptions); app.useoauthbearerauthentication(new oauthbearerauthenticationoptions()); } }
iocconfig
public static class iocconfig { public static void register(httpconfiguration config) { var builder = new containerbuilder(); // configure container // register individual components builder.register(c => new mysqlcontext()).as<imysqlcontext>().instanceperrequest(); builder.registertype<simpleauthorizationserverprovider>().as<ioauthauthorizationserverprovider>(); builder.registerapicontrollers(assembly.getexecutingassembly()); var container = builder.build(); config.dependencyresolver = new autofacwebapidependencyresolver(container); } }
you have lot of code there, it's not easy isolate problem. first step, consider removing code autofac di , see if makes difference. it's hard tell problem might otherwise.
if issue indeed related di code, perhaps should raised separate question. in case, seek create little code illustration demonstrates issue succinctly. people more help if problem code short , point.
owin asp.net-web-api2
No comments:
Post a Comment