我正在尝试将用户角色实现到我的MVC Web应用程序中。但是,我在帐户控制器的return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
行上获得了一个空例外。
帐户控制器
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private ApplicationRoleManager _roleManager;
public AccountController(){}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ApplicationRoleManager roleManager)
{ UserManager = userManager;
SignInManager = signInManager;
RoleManager = roleManager; }
public ApplicationRoleManager RoleManager
{
get { return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); }
private set { _roleManager = value; }
}
public ApplicationSignInManager SignInManager
{
get { return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); }
private set { _signInManager = value; }
}
public ApplicationUserManager UserManager
{
get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
private set {_userManager = value; }
}
ActionResult将触发将用户添加到角色。
[System.Web.Mvc.HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
ApplicationUser user = context.Users.FirstOrDefault(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase));
var account = new AccountController();
if (user != null) account.UserManager.AddToRole(user.Id, RoleName);
var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
ViewBag.Roles = list;
return View("ManageUserRoles");
}
Startup.Auth确实包含
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
身份配置
// Configure the application sign-in manager which is used in this application.
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{ return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager); }
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{ return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication); }
}
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
: base(roleStore) { }
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
}
}
空引用来自哪里?在代码中,我仔细检查用户是否存在。
由于
答案 0 :(得分:19)
这些无疑是违法行......
var account = new AccountController();
if (user != null) account.UserManager.AddToRole(user.Id, RoleName);
控制器并不意味着以这种方式实例化,因为它们与当前的HTTP请求密切相关(因此HttpContext
)。
当HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
被点击时,HttpContext
为空,因为没有上下文。
您可以在控制器中将相同的属性放在您尝试访问UserManager的位置。这是有效的,因为OwinContext在整个应用程序中共享。
public class HomeController : Controller
{
public ApplicationUserManager UserManager
{
get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
private set {_userManager = value; }
}
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
ApplicationUser user = context.Users.FirstOrDefault(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase));
if (user != null) UserManager.AddToRole(user.Id, RoleName);
//Other code...
return View("ManageUserRoles");
}
}
如果你想变得非常花哨,请声明一个继承自BaseController
的{{1}},将Controller
属性放入其中,让所有其他控制器继承自你的基础。< / p>