asp.net MVC4 プロジェクトでプロバイダーなしで Role を使用したい。
プロバイダーなしでログインおよびログアウトすることに成功しました。
しかし、役割については、設定方法がわかりません。
これは AccountController.cs の私の部分です
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && myLogin(model.UserName, model.Password, persistCookie: model.RememberMe))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
//set Roles, how can I set role to users?
if (HttpContext.User != null)
{
if (!Roles.RoleExists("Administrator"))
{
Roles.CreateRole("Administrator");
}
Roles.AddUserToRole(model.UserName, "Administrator");
//string[] roles = { "Moderator", "Administrator" };
//HttpContext.User = new GenericPrincipal(HttpContext.User.Identity, roles);
}
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
private bool myLogin(string userName, string password, bool persistCookie)
{
if (userName == "root" && password == "root")
{
return true;
}
else
{
return false;
}
}
// set Roles 部分が機能していません。エラー メッセージは次のように表示されます。
「root」という名前のユーザーは見つかりませんでした。説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。例外の詳細: System.InvalidOperationException: "root" という名前のユーザーが見つかりませんでした。
Web.config
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<roleManager enabled="true" />
このページへのアクセスをロールチェックで制御したいのですが、
[Authorize(Roles="Administrator")]
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
ユーザーにロールを定義するにはどうすればよいですか? アクションでフィルターを使用するには?