C# MVC5帐户控制器空引用异常
我正试图在我的MVC web应用程序中实现用户角色。然而,我在C# MVC5帐户控制器空引用异常,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我正试图在我的MVC web应用程序中实现用户角色。然而,我在return\u userManager?一行中得到一个空异常??HttpContext.GetOwinContext().GetUserManager()在我的帐户控制器中 账户控制员 [Authorize] public class AccountController : Controller { private ApplicationSignInManager _signInManager; private Ap
return\u userManager?一行中得到一个空异常??HttpContext.GetOwinContext().GetUserManager()代码>在我的帐户控制器中
账户控制员
[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; }
}
Auth不包含
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext(ApplicationRoleManager.Create);
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(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>()));
}
}
//配置此应用程序中使用的应用程序登录管理器。
公共类应用程序SignInManager:SignInManager
{
公共应用程序签名管理器(ApplicationUserManager用户管理器、IAAuthenticationManager authenticationManager)
:base(userManager、authenticationManager)
{
}
公共覆盖任务CreateUserIdentityAsync(ApplicationUser用户)
{返回用户.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);}
公共静态应用程序SigninManager创建(IdentityFactoryOptions选项,IOwinContext上下文)
{返回新的ApplicationSignInManager(context.GetUserManager(),context.Authentication);}
}
公共类应用程序RoleManager:RoleManager
{
公共应用程序角色管理器(IRoleStore角色存储库)
:base(roleStore){}
公共静态应用程序角色管理器创建(IdentityFactoryOptions选项,IOwinContext上下文)
{
返回新的ApplicationRoleManager(新的RoleStore(context.Get());
}
}
空引用来自哪里?在代码中,我反复检查用户是否存在
谢谢这些无疑是令人不快的台词
var account = new AccountController();
if (user != null) account.UserManager.AddToRole(user.Id, RoleName);
控制器不应该以这种方式实例化,因为它们与当前的HTTP请求紧密相连(因此HttpContext
)
当HttpContext.GetOwinContext().GetUserManager()时代码>被命中,HttpContext
为空,因为没有上下文
您可以简单地将相同的属性放在您试图访问UserManager的控制器中。这是因为文本在整个应用程序中共享
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");
}
}
公共类HomeController:控制器
{
公共应用程序管理员用户管理器
{
获取{return\u userManager??HttpContext.GetOwinContext().GetUserManager();}
私有集{u userManager=value;}
}
public ActionResult RoleAddToUser(字符串用户名、字符串角色名)
{
ApplicationUser user=context.Users.FirstOrDefault(u=>u.UserName.Equals(UserName,StringComparison.CurrentCultureIgnoreCase));
if(user!=null)UserManager.AddToRole(user.Id,RoleName);
//其他代码。。。
返回视图(“ManageUserRoles”);
}
}
如果你真的想变得有趣,可以声明一个继承自Controller
的BaseController
,将UserManager
属性放在里面,让你所有的其他控制器继承自你的BaseController。天哪,这也太简单了。非常感谢!这一次我也被抓住了。走了一条奇特的路线,创建了一个基本控制器,将通用函数传递到我需要的其他地方。聪明的想法!
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");
}
}