c# - HttpContext.GetOwinContext().GetUserManager() return null -
c# - HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() return null -
i've used asp.net identity 2 creating role result of httpcontext.getowincontext().getusermanager<approlemanager>()
null.
then couldn't create role.
how can solve problem?
public class mvccontroller : controller { public mvccontroller() { } public approlemanager rolemanager // returns null ?! { { homecoming httpcontext.getowincontext().getusermanager<approlemanager>(); } } public user currentuser { { string currentuserid = user.identity.getuserid(); user currentuser = datacontextfactory.getdatacontext().users.firstordefault(x => x.id.tostring() == currentuserid); homecoming currentuser; } } public iauthenticationmanager authmanager { { homecoming httpcontext.getowincontext().authentication; } } public appusermanager usermanager { { homecoming httpcontext.getowincontext().getusermanager<appusermanager>(); } } }
my controller:
public class roleadmincontroller : mvccontroller { [httppost] public async task<actionresult> createrole([required]string name) { if (modelstate.isvalid) { if (rolemanager != null) // rolemanager null, why?! { identityresult result = await rolemanager.createasync(new role { name = name }); if (result.succeeded) { homecoming redirecttoaction("index"); } else { adderrorsfromresult(result); } } } homecoming view(name); } }
approlemanager:
public class approlemanager : rolemanager<role, int>, idisposable { public approlemanager(rolestore<role, int, userrole> store) : base(store) { } public static approlemanager create(identityfactoryoptions<approlemanager> options, iowincontext context) { homecoming new approlemanager(new rolestore<role, int, userrole>(datacontextfactory.getdatacontext())); } }
most have missed giving owincontext way create applicationusermanager. you'll need have these in public void configuration(iappbuilder app)
app.createperowincontext<applicationusermanager>(applicationusermanager.create); app.createperowincontext<approlemanager>(approlemanager.create);
this register delegates create usermanager
, rolemanager
owincontext
, after can phone call these in controllers.
c# asp.net-identity identity roles owin
Comments
Post a Comment