我正在关注如何在C#MVC中以编程方式向用户添加角色的this page上的教程。但是,当代码执行时,我收到以下错误:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=UserId not found.
Source=Microsoft.AspNet.Identity.Core
StackTrace:
at Microsoft.AspNet.Identity.UserManager`2.<AddToRoleAsync>d__83.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.AspNet.Identity.AsyncHelper.RunSync[TResult](Func`1 func)
at Microsoft.AspNet.Identity.UserManagerExtensions.AddToRole[TUser,TKey](UserManager`2 manager, TKey userId, String role)
at SchoolRegistration.Controllers.AccountController.RoleAddToUser(FormCollection collection) in c:\Users\Kyle\Documents\Visual Studio 2013\Projects\SchoolRegistration\SchoolRegistration\Controllers\AccountController.cs:line 98
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
InnerException:
但我无法弄清楚导致此错误的原因。任何帮助将不胜感激。
代码:
在我的帐户控制器中:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(FormCollection collection)
{
string UserName = collection["UserName"];
string RoleName = collection["RoleName"];
using (var context = new ApplicationDbContext()) {
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var user2 = new ApplicationUser() { UserName = UserName };
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(user2.Id, RoleName);
var account = new AccountController();
//account.UserManager.AddToRole(user.Id, RoleName);
ViewBag.ResultMessage = "Role added successfully.";
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");
}
更新
好的,所以在听取了大家的建议后,我将代码改写为如下所示:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(FormCollection collection)
{
string UserName = collection["UserName"];
string RoleName = collection["RoleName"];
using (var context = new ApplicationDbContext()) {
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var user2 = UserManager.FindByNameAsync(UserName);
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(user2.Id.ToString(),RoleName);
var account = new AccountController();
//account.UserManager.AddToRole(user.Id, RoleName);
ViewBag.ResultMessage = "Role added successfully.";
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");
}
奇怪的是,我得到了完全相同的错误。但是,当我现在将鼠标悬停在我的参数上时,它确实具有有效的id而不是null:
我确实注意到调试时收到的值与数据库中的值不一样(这可能是因为哈希安全性?)
DB值:0752a8d8-fb0e-44d9-b8b0-57ccbbff826b
更新2:
对于任何与id不匹配有相同问题的人,请尝试使用FindByName方法而不是FindByNameAsync。为我工作!
答案 0 :(得分:2)
错误消息告诉您问题:
消息=未找到UserId。
您永远不会设置user2.Id所以当您调用AddToRole时,它找不到要添加角色的用户,因为user2.Id为空。
您在此处设置user2:
var user2 = new ApplicationUser() { UserName = UserName };
然后你在这里引用user2.Id
,但它没有被设置。
userManager.AddToRole(user2.Id, RoleName);
首先尝试按ID查找用户。