dependency injection - How can I abstract dependent type from base C# base project using microsoft identity framework? -


i have authentication project reuse.

i have aspnetcore identity project called authentication intended reusable authentication project. if have looked @ aspnetcore identity implementation see type called usermanager application user class store implementation of user in aspnetusers database table.

public class applicationuser : identityuser { } 

the problem having there controller in isolated authentication project called accountcontroller holds logging in/out, registering, , other related account actions. wish abstract application user out of project's class can change based on needs of project multiple solutions.

the authentication project has startup class initiated follows within project in being used:

authenticationstartup.configureservices<applicationuser, mydatabase>       (services, configuration); 

as can see, subscribing project adds own implementation of applicationuser. next, comes configuration of identity references tapplicationuser in services.addidentity call.

    public void configureservices<tapplicationuser, tcontext>           (iservicecollection services, iconfigurationroot configuration)         tapplicationuser : identityuser         tcontext : dbcontext     {         services.addidentity<tapplicationuser, identityrole>()             .addentityframeworkstores<tcontext>()             .adddefaulttokenproviders(); 

unfortunately, breaks down in accountcontroller using injected service. how, can tapplicationuser added controller.

public class accountcontroller : controller {     private readonly usermanager<tapplicationuser> _usermanager;      public accountcontroller(usermanager<tapplicationuser> usermanager)      {         _usermanager = usermanager;     } } 

this broken since there no tapplicationuser. furthermore, if add tapplicationuser follows controller nolonger resolved , nothing happens.

public class accountcontroller<tapplicationuser> : controller     tapplicationuser : identityuser {     private readonly usermanager<tapplicationuser> _usermanager;      public accountcontroller(usermanager<tapplicationuser> usermanager)      {         _usermanager = usermanager;     } }  

is there anyway application controller still resolved included type parameters?

and also, have found problem in if add type parameter in base class how able add type parameter in views use tapplicationuser. here example

view embedded in authentication project

@* tapplicationuser doesn't resolve *@ @inject signinmanager<tapplicationuser> signinmanager   @{     var loginproviders = signinmanager.getexternalauthenticationschemes().tolist();     if (loginproviders.count == 0)     {         <div>             <p>                 there no external authentication services configured. see <a href="https://go.microsoft.com/fwlink/?linkid=532715">this article</a>                 details on setting asp.net application support logging in via external services.             </p>         </div>     }     else     {         <form asp-controller="account" asp-action="externallogin" asp-route-returnurl="@viewdata["returnurl"]" method="post" class="form-horizontal">             <div>                 <p>                     @foreach (var provider in loginproviders)                     {                         <button type="submit" class="btn btn-default" name="provider" value="@provider.authenticationscheme" title="log in using @provider.displayname account">@provider.authenticationscheme</button>                     }                 </p>             </div>         </form>     } } 

have generic controller base controller lives in reusable project

public abstract class accountcontrollerbase<tapplicationuser> : controller      tapplicationuser : identityuser {     protected readonly usermanager<tapplicationuser> _usermanager;      protected accountcontroller(usermanager<tapplicationuser> usermanager) {         _usermanager = usermanager;     } } 

the using class/project derive base controller , define generic argument

public class accountcontroller : accountcontrollerbase<applicationuser> {      public accountcontroller(usermanager<applicationuser> usermanager)          : base(usermanager) {  }  } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -