尝试将用户添加到角色,System.Web.HttpContextBaseExtensions.GetOwinEnvironment 未找到
Trying to add user to role, System.Web.HttpContextBaseExtensions.GetOwinEnvironment not found
我的RoleController
:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var account = new AccountController();
account.UserManager.AddToRole(user.Id, RoleName);
ViewBag.ResultMessage = "Role created successfully !";
// prepopulat roles for the view dropdown
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");
}
我的AccountController
:
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
}
}
上面的最后一行是中断的地方。我只是想将用户添加到角色。
对象引用未设置为对象的实例。
[NullReferenceException: Object reference not set to an instance of an
object.]
System.Web.HttpContextBaseExtensions.GetOwinEnvironment(HttpContextBase
context) +34
System.Web.HttpContextBaseExtensions.GetOwinContext(HttpContextBase
context) +50
编辑:
我认为这个问题比 "possible duplicate of another question" 中提到的任何问题都更深。我认为我什至没有正确的参考工作,即使我在解决方案资源管理器的参考中看到它们,尝试重新添加它们,并尝试使用 NugGet 来 import/update 它们。
不确定这是怎么发生的或为什么会发生,但不知何故我没有引用 OwinContext,这可能会导致跟踪返回未设置为实例的对象引用。
我什至不知道如何解释这个 MVC/Owin/EF/references 正在慢慢让我想念 MS Access、Visual Basic 和 DLL Hell。
所有这些疯狂可能都有一些方法。
在我想的那天把一切都搁置之后,"Well, Register and Forgot Password, and Login are working, why don't I just see if I can add a user to role by hand and then see if I can create my own code to add the user at login since I won't be doing much 'assigning user to role' afterwards?")
所以感谢 Google 最后但并非最不重要的 Whosebug 和 this link,我不仅能够在他们注册时将用户添加到角色,而且我能够复制和粘贴一些AddToRole 行正上方的代码给我错误。和 。 . .有效!这是我添加的代码。我更改了一些内容,因为 Register 创建了一个新用户,但 AddToRole 使用的是现有用户。所以我不需要新的命名空间,只需要来自比我聪明得多的人的新知识,这是肯定的。
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
var context = new ApplicationDbContext();
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var account = new AccountController();
// var user = new ApplicationUser { UserName = UserName, Email = UserName };
//var result = await UserManager.CreateAsync(user, model.Password);
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(user.Id, RoleName);
ViewBag.ResultMessage = "Role created successfully !";
我的RoleController
:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var account = new AccountController();
account.UserManager.AddToRole(user.Id, RoleName);
ViewBag.ResultMessage = "Role created successfully !";
// prepopulat roles for the view dropdown
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");
}
我的AccountController
:
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
}
}
上面的最后一行是中断的地方。我只是想将用户添加到角色。
对象引用未设置为对象的实例。
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.HttpContextBaseExtensions.GetOwinEnvironment(HttpContextBase context) +34
System.Web.HttpContextBaseExtensions.GetOwinContext(HttpContextBase context) +50
编辑: 我认为这个问题比 "possible duplicate of another question" 中提到的任何问题都更深。我认为我什至没有正确的参考工作,即使我在解决方案资源管理器的参考中看到它们,尝试重新添加它们,并尝试使用 NugGet 来 import/update 它们。
不确定这是怎么发生的或为什么会发生,但不知何故我没有引用 OwinContext,这可能会导致跟踪返回未设置为实例的对象引用。
我什至不知道如何解释这个 MVC/Owin/EF/references 正在慢慢让我想念 MS Access、Visual Basic 和 DLL Hell。
所有这些疯狂可能都有一些方法。
在我想的那天把一切都搁置之后,"Well, Register and Forgot Password, and Login are working, why don't I just see if I can add a user to role by hand and then see if I can create my own code to add the user at login since I won't be doing much 'assigning user to role' afterwards?")
所以感谢 Google 最后但并非最不重要的 Whosebug 和 this link,我不仅能够在他们注册时将用户添加到角色,而且我能够复制和粘贴一些AddToRole 行正上方的代码给我错误。和 。 . .有效!这是我添加的代码。我更改了一些内容,因为 Register 创建了一个新用户,但 AddToRole 使用的是现有用户。所以我不需要新的命名空间,只需要来自比我聪明得多的人的新知识,这是肯定的。
public ActionResult RoleAddToUser(string UserName, string RoleName)
{
var context = new ApplicationDbContext();
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var account = new AccountController();
// var user = new ApplicationUser { UserName = UserName, Email = UserName };
//var result = await UserManager.CreateAsync(user, model.Password);
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(user.Id, RoleName);
ViewBag.ResultMessage = "Role created successfully !";