c# - What's the best way to keep access to the ApplicationUserManager after the controller has been disposed? -


ran interesting problem yesterday. mvc website identity 1 used iprincipal extension method custom authentication, worked fine across razor views needed. identity 2, need access in extension method applicationusermanager, i've been forced pass httpcontext.current through views.

this works fine, apart inside razor @section sections - following call stack, appears these don't rendered until after controller has been disposed, disposes database , user managers etc.

what best way around limitation? want keep using sections, let me inject javascript down bottom of html, want keep using razor extensions inside these sections nice, easy way control gets outputted page.

at moment i'm being forced create new applicationusermanager , mydbcontext, stuff them in viewbag , pass them along extension, isn't ideal.

example of extension method:

public static bool haspermission(this iprincipal source, applicationusermanager um, mydbcontext db, string controller, string method) {         if (!source.identity.isauthenticated) {             return false;         }          var roles = um.getroles(source.identity.getuserid());         if (roles.count == 0) {             return false;         }          // admins have global access         string role = roles[0];         if (role.equals("admin")) {             return true;         }         // fine being un-domained, permissions global         permissionmatrix permission = db.permissionmatrices.firstordefault(x => x.controller == controller && x.action == method && x.rolename == role);         return permission != null;     } 

example of it's used:

@if (user.haspermission((applicationusermanager)viewbag.usermanager, (mydbcontext)viewbag.database, "event", "create")) {     @html.actionlink("create new", "create") } 

editing make problem more clear:

this extension method before put hack in -

public static bool haspermission(this iprincipal source, httpcontext context, string controller, string method) {     var usermanager = context.getowincontext().get<applicationusermanager>();     var mydb = context.getowincontext().get<mydbcontext>(); 

like kind of said above, works (passing httpcontext.current through .cshtml page), unless call inside razor @section, fails, because these run after controller has been disposed, disposes stuff inside owincontext.

the code above hacky way i've got working @ moment; in controllers have create mydbcontext , applicationusermanager.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -