0
ログインページにメンバーシップと役割のプロバイダを初めて使用しています。私のメンバーシップはうまくいきましたが、ログインページでロールプロバイダを使用できませんでした。 MyAccountコントローラという名前のコントローラが1つあります。このコントローラーはユーザーメンバーシップを確認し、検証後、ユーザーの役割に基づいてホームコントローラーにリダイレクトされます。MVC 5でロールプロバイダを使用するには?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(Login l, string returnUrl = "")
{if (ModelState.IsValid)
{
var isValidUser = Membership.ValidateUser(l.UserName, l.Password);
if (isValidUser)
{
FormsAuthentication.SetAuthCookie(l.UserName, l.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else If(*"USER ROLE AS ADMIN"*)
{
RedirectToAction("AdminIndex","Home");
}
else
{
RedirectToAction("ClientIndex","Home");
}
}
}
ViewBag.ErrorMassage = "Wrong Id or Password";
ModelState.Remove("Password");
return View();
}
そして
ホームコントローラ: ここ
マイアカウントコントローラである
[Authorize (Roles= "Admin")] public ActionResult AdminIndex() { return View(); } [Authorize (Roles = "Client")] public ActionResult ClientIndex() { return View(); }
Iわからないどこでユーザーの役割、MyAccountコントローラまたはHome Controllerをチェックする必要がありますか?
RoleProvider:
public override string[] GetRolesForUser(string username) { if (!HttpContext.Current.User.Identity.IsAuthenticated) { return null; } //check cache var cacheKey = string.Format("{0}_role", username); if (HttpRuntime.Cache[cacheKey] != null) { return (string[])HttpRuntime.Cache[cacheKey]; } string[] roles = new string[] { }; roles = gateway.GetUserRole(username); { if (roles.Any()) { HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration); } } return roles; } public override bool IsUserInRole(string username, string roleName) { var userRoles = GetRolesForUser(username); return userRoles.Contains(roleName); }
どのように私は私のコントローラにこのRoleProviderを使用すると、管理者またはクライアントのアクションにリダイレクトすることができますか?