Web API 2 OWIN Bearer token authentication with Dependency Injection Not working -


i need create web api 2 project token based authentication , implement dependency injection in controllers including default accountcontroller.

following these steps

  • using visual studio 2013
  • create new asp.net web application (framework 4.5)
  • select web api
  • authentication set individual user accounts

i referred services , contracts class dlls , packages

pm > install-package unity

pm > install-package unity.webapi

altered unity.config

public static void registercomponents() {     var container = new unitycontainer();          // register components container here         // not necessary register controllers          // e.g. container.registertype<itestservice, testservice>();         var section = loadunityconfigurationfile();         section.configure(container, "parent");         ////section.containers.default.configure(container);         container.registerinstance(typeof(iunitycontainer), container);          globalconfiguration.configuration.dependencyresolver = new unitydependencyresolver(container);     }    private static unityconfigurationsection loadunityconfigurationfile()     {         var section = (unityconfigurationsection)configurationmanager.getsection("unity");         return section;     } 

i have added unity.config file under folder config mapping controllers , dlls (that has been specified in above code)

<containers>         <container name="parent">           <types>             <type type= "proj.api.services.data.iasuser, proj.api.services"                 mapto="proj.api.services.data.asuser, proj.api.services"></type>             <type type="proj.api.services.contracts.iuserservices, proj.api.services.contracts"mapto="proj.api.services.business.userservices, proj.api.services"></type>             <type type="proj.api.controllers.accountcontroller, proj.api"                   mapto="proj.api.controllers.accountcontroller, proj.api" ></type>             <type type="proj.api.controllers.homecontroller, proj.api"                   mapto="proj.api.controllers.homecontroller, proj.api" ></type>            </types>         </container>       </containers> 

i added controller usercontroller getting user details. thats working fine

public class usercontroller : apicontroller         {              public usercontroller(iunitycontainer container)             {                 this.unitycontainer = container;             }             public iunitycontainer unitycontainer { get; set; }              [httpget]             [route("get")]             public serviceresponsedto<userdto> getuserdetails()             {                 var userservices = this.unitycontainer.resolve<iuserservices>();                 return userservices.getuserdetails();             }     } 

after adding these control, default accountcontroller has stopped working.

when uncomment line unityconfig.registercomponents(); global.asax, that's working fine

protected void application_start()     {         arearegistration.registerallareas();         //unityconfig.registercomponents();           globalconfiguration.configure(webapiconfig.register);         routeconfig.registerroutes(routetable.routes);         bundleconfig.registerbundles(bundletable.bundles);     } 

how add dependency injection in accountcontroller ?

 namespace aub.api.controllers     {         [authorize]         [routeprefix("api/account")]         public class accountcontroller : apicontroller         {             private const string localloginprovider = "local";             private applicationusermanager _usermanager;              public accountcontroller()             {             }              public accountcontroller(applicationusermanager usermanager,             isecuredataformat<authenticationticket> accesstokenformat)             {                 usermanager = usermanager;                 accesstokenformat = accesstokenformat;             }              public applicationusermanager usermanager             {                                 {                 return _usermanager ?? request.getowincontext().getusermanager<applicationusermanager>();                 }                 private set                 {                 _usermanager = value;                 }             }              public isecuredataformat<authenticationticket> accesstokenformat { get; private set; }              // api/account/userinfo             [hostauthentication(defaultauthenticationtypes.externalbearer)]             [route("userinfo")]             public userinfoviewmodel getuserinfo()             {                 externallogindata externallogin = externallogindata.fromidentity(user.identity claimsidentity);                  return new userinfoviewmodel                 {                     email = user.identity.getusername(),                     hasregistered = externallogin == null,                     loginprovider = externallogin != null ? externallogin.loginprovider : null                 };             }         }     } 

i need account related functionalities in accountcontroller including dependency injection. please help


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -