我使用了https://github.com/rustd/AspnetIdentitySample中提供的示例来创建自定义用户配置文件数据。
然而,我不确定如何在注册时为新注册的用户分配角色。看起来它使用了.net 4.5基于声明的角色。此外,它还使用代码优先迁移,并创建必要的成员资格表,如角色、UserRoles。
我希望将新角色添加到Roles表中,并将此角色分配给具有UserRoles表条目的新用户。
真的很感谢你的帮助。
发布于 2013-08-17 04:41:03
例如,如果您想创建角色并向其中添加新用户,您可以修改Register方法,如下所示:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
WebSecurity.Login(model.UserName, model.Password);
Roles.CreateRole("Role A");// add "Role A"
Roles.AddUserToRole(model.UserName, "Role A"); // user in role A
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
return View(model);
}发布于 2013-09-07 00:29:58
试试IdentityConfig.Roles.AddUserToRole?确保角色在DefaultConnection的角色表中。我猜你正在制作他们在今年夏天构建之前的会议上发布的演示?Andrey的答案依赖于旧的会员系统,我相信是带有web配置实用程序的?
发布于 2014-01-25 19:54:42
var rm =新的RoleManager(RoleStore(newApplicationDbContext();
Rm.Create(新管理员(“IdentityRole”));
Rm.Create(新IdentityRole(“财经”));
UserManager.AddToRole(user.Id,“管理员”);
https://stackoverflow.com/questions/18279335
复制相似问题