そのため、現在、ログインに設定された ViewBag を使用して、管理者のみのものを表示できるかどうかを判断しています。これは、ModelFirst ASP.net を使用しているため、Roles.CreateRole、Membership.CreateUser、および Roles.AddUserToRole が無効になっているためです。
public ActionResult Login(LoginModel model, string returnUrl)
{
ViewBag.Admin = false;
if (model.IsValid(model.UserName, model.Password))
{
ViewBag.Admin = (bool)model.currentLoggedInEmployee.IsAdmin;
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login data is incorrect!");
return View(model);
}
}
次に、単純に次を使用します。
@if (ViewBag.Admin == true) {
<li>@Html.ActionLink("Administration", "Index", "Administration")</li>
}
これらのボタンを管理者にのみ表示します。これは機能します。
ここで私たちが望むのは、通常の
[Authenticate(Roles="Admin")]
[HttpPost]
public ActionResult Create(FormCollection collection)
{
// TODO: Add insert logic here
}
しかし、「ロール」がないため、このようにすることはできません。ViewBag.Admin 値を使用して、ユーザーがこれらの機能を使用できるようにする必要があります。問題は、これをどのように行うことができるかということです。