本文介绍了。HttpContext.GetOwinContext()GetUserManager< AppRoleManager>()返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET身份2,用于创建角色,但 HttpContext.GetOwinContext()GetUserManager<的结果。AppRoleManager>()为null。

然后,我无法创建角色。



我该如何解决这个问题呢?

 公共类MVCController:控制器
{
公共MVCController()
{

}
酒店的公共AppRoleManager RoleManager //返回null?!
{
得到
{
返回HttpContext.GetOwinContext()GetUserManager< AppRoleManager>();
}
}
公众用户的currentUser
{
得到
{
串currentUserId = User.Identity.GetUserId();
用户的currentUser = DataContextFactory.GetDataContext()Users.FirstOrDefault(X => x.Id.ToString()== currentUserId)。
回报率的currentUser;
}
}
公共IAuthenticationManager AuthManager
{
得到
{

返回HttpContext.GetOwinContext()认证。
}
}
公共AppUserManager的UserManager
{
得到
{
返回HttpContext.GetOwinContext()GetUserManager< AppUserManager>();
}
}
}



我的控制器:

 公共类RoleAdminController:MVCController 
{
[ HttpPost]
公共异步任务<&的ActionResult GT; CREATEROLE([必填]字符串名称)
{
如果(ModelState.IsValid)
{
如果(RoleManager!= NULL)// RoleManager是空的,为什么?
{
IdentityResult结果=等待RoleManager.CreateAsync(新角色{名称=名称});
如果(result.Succeeded)
{
返回RedirectToAction(「指数」);
}
,否则
{
AddErrorsFromResult(结果);
}
}
}
返回查看(名称);
}
}



AppRoleManager:

 公共类AppRoleManager:RoleManager<作用,INT>中IDisposable的
{
公共AppRoleManager(Rolestore的<作用,INT的UserRole>存储)
:基(存储)
{
}
公共静态AppRoleManager创建(IdentityFactoryOptions< AppRoleManager>选项,IOwinContext上下文)
{
返回新AppRoleManager(新Rolestore的<作用,INT的UserRole>(DataContextFactory.GetDataContext()));
}
}


解决方案

最可能你已经错过了给OwinContext的方式创建ApplicationUserManager。结果
对于你需要有这些在你的公共无效配置(IAppBuilder应用)

  app.CreatePerOwinContext< ApplicationUserManager>(ApplicationUserManager.Create); 
app.CreatePerOwinContext< AppRoleManager>(AppRoleManager.Create);

这将注册,创造代表的UserManager RoleManager OwinContext 后,才可以在控制器中调用这些了。

I've used ASP.NET Identity 2 for creating role but the result of HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() was null.

Then I couldn't create the role.

How can I solve this problem?

public class MVCController : Controller
{
    public MVCController()
    {

    }
    public AppRoleManager RoleManager // returns null ?!
    {
        get
        {
            return HttpContext.GetOwinContext().GetUserManager<AppRoleManager>();
        }
    }
    public User CurrentUser
    {
        get
        {
            string currentUserId = User.Identity.GetUserId();
            User currentUser = DataContextFactory.GetDataContext().Users.FirstOrDefault(x => x.Id.ToString() == currentUserId);
            return currentUser;
        }
    }
    public IAuthenticationManager AuthManager
    {
        get
        {

            return HttpContext.GetOwinContext().Authentication;
        }
    }
    public AppUserManager UserManager
    {
        get
        {
            return 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 is null, why?!
            {
                IdentityResult result = await RoleManager.CreateAsync(new Role { Name = name });
                if (result.Succeeded)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
        }
        return 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)
    {
        return new AppRoleManager(new RoleStore<Role, int, UserRole>(DataContextFactory.GetDataContext()));
    }
}
解决方案

Most likely you have missed giving OwinContext the way to create ApplicationUserManager.
For that you'll need to have these in your public void Configuration(IAppBuilder app)

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create);

This will register delegates that create UserManager and RoleManager with OwinContext and only after that you can call these back in your controllers.

这篇关于。HttpContext.GetOwinContext()GetUserManager&LT; AppRoleManager&GT;()返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:26