我已經使用https://github.com/rustd/AspnetIdentitySample中給出的示例來創建自定義用戶個人資料數據。身份會員角色ASP.NET 4.5 MVC添加基於角色的聲明
但是,我不確定如何在註冊後爲新註冊的用戶分配角色。它看起來像它使用.net 4.5基於角色的聲明。它還使用Code First - 遷移並創建必要的成員表,如角色,UserRoles。
我正在尋找將角色添加到角色表中,並將此角色分配給具有UserRoles表中的條目的新用戶。
幫助真的很感激。
[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);
}
之前就試過了。沒有幫助。提供「角色管理器功能尚未啓用」角色管理器功能尚未啓用 描述:執行當前Web請求期間發生未處理的異常請查看堆棧跟蹤以獲取有關錯誤的更多信息及其起源的位置 異常詳細信息:System.Configuration.Provider.ProviderException:角色管理器功能尚未啓用 – user2690280
@ user2690280僅向web.config添加 –
嘗試添加 但是也沒有幫助,如果你看一下示例項目(https://github.com/rustd/AspnetIdentitySample),有一個單獨的成員數據庫處理用戶。提供者的東西 –
user2690280