我的问题是我正在尝试创建'角色'并将其添加到用户中。它在对Usermanager类的引用中出错,如下所示
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);
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");
和我的帐户controller如下
public class AccountController : Controller
{
public UserManager<ApplicationUser, string> um = new UserManager<ApplicationUser, string>(ApplicationUser user, string key);
我也尝试了
public UserManager<ApplicationUser, string> um = new UserManager<>();
和前者都说出两个错误:
超载方法具有一些无效的争论
,后者说:
类型预期
我也尝试了
public UserManager<ApplicationUser, string> um = new UserManager<ApplicationUser,string>();
这给出了错误:
此方法不包含取0参数的构造函数
如何正确实现/呼叫/使用UserManager类?
根据msdn https://msdn.microsoft.com/en-us/library/dn497520(v = vs.108).aspx
usermanager具有以下构造函数定义
public UserManager( IUserStore<TUser, TKey> store)
因此,当您initiliaze IT时,您需要先将IuserStore实例如下如下
您需要实现iuserstore
public class MyUserStore<T> : IUserStore<T> where T: ApplicationUser { }
创建Myuserstore的实例
MyUserStore userStore = new MyUserStore<ApplicationUser>();
创建您的用户管理器
UserManager<ApplicationUser, string> um = new UserManager(userStore);