c# - Token based authentication in repository pattern project -


i have created project based on repository pattern. , used tutorial implement token based authentication tutorial link here.

here's code:

valuescontroller.cs

public ihttpactionresult getauth(string username, string password)     {         return ok(_au.authenticate(username, password)); //returns true or false     } 

iauthenticator.cs

namespace authso.core.interface {     public interface iauthenticator:idisposable     {         bool authenticate(string username, string password);     } } 

authenticator.cs

namespace authso.core.manager {     public class authenticator : iauthenticator, idisposable     {         public igetdata _get;         public authenticator(igetdata _get)         {             this._get = _get;         }         public bool authenticate(string username, string password)         {             return _get.authenticate(username, password);         }         public void dispose()         {             //left empty here, nothing dispose here guess         }     } } 

authenticatorprovider.cs

namespace authso.api.provider {     public class authenticatorprovider : oauthauthorizationserverprovider     {         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[] { "*" });              using (iauthenticator _auth = new authenticator()) //i'm getting error here             {                 bool result = _auth.authenticate(context.username, context.password);                  if (!result)                 {                     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);          }     } } 

issue :

enter image description here

how can solve issue. i'm using autofac dependency injection.

my sample project issue in github repo

any advice helpful, thank you.

update #1:

i tried using idisposable still gets me error, please see updated authenticator.cs code. (tokenauthissue/authso.core/manager/authenticator.cs in github)

enter image description here

update #2:

i tried using this

using (iauthenticator _auth = new authenticator(new getdata())) 

but again got error

<error> <message>an error has occurred.</message> <exceptionmessage> error occurred when trying create controller of type 'valuescontroller'. make sure controller has parameterless public constructor. </exceptionmessage> <exceptiontype>system.invalidoperationexception</exceptiontype> <stacktrace> @ system.web.http.dispatcher.defaulthttpcontrolleractivator.create(httprequestmessage request, httpcontrollerdescriptor controllerdescriptor, type controllertype) @ system.web.http.controllers.httpcontrollerdescriptor.createcontroller(httprequestmessage request) @ system.web.http.dispatcher.httpcontrollerdispatcher.<sendasync>d__1.movenext() </stacktrace> <innerexception> <message>an error has occurred.</message> <exceptionmessage> type 'authso.api.controllers.valuescontroller' not have default constructor </exceptionmessage> <exceptiontype>system.argumentexception</exceptiontype> <stacktrace> @ system.linq.expressions.expression.new(type type) @ system.web.http.internal.typeactivator.create[tbase](type instancetype) @ system.web.http.dispatcher.defaulthttpcontrolleractivator.getinstanceoractivator(httprequestmessage request, type controllertype, func`1& activator) @ system.web.http.dispatcher.defaulthttpcontrolleractivator.create(httprequestmessage request, httpcontrollerdescriptor controllerdescriptor, type controllertype) </stacktrace> </innerexception> </error> 

this dependency injection issue. please guide me solution.

looks iauthenticator has not inherited idisposable. when using 'using' idisposable makes sure can dispose variable using statement after using scope has ended.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -