0

私の課題は、ロールに基づいてユーザーをエリアにリダイレクトすることです。あるエリアにリダイレクトする矢印を配置しましたが、ロールがこのロールタイプではない場合、別のエリアを処理するために例外を配置する必要があります。

デフォルトの LogOn Controller アクションを変更するにはどうすればよいですか。

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                  return RedirectToAction("Index", "Home", new { area = "Client" });

                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
4

2 に答える 2

1

私はそれを考え出した。この率直な回答を含む投稿はまったくありませんでしたので、お気軽に再投稿してください。

        [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    if (Roles.IsUserInRole(model.UserName, "UserRoleOne"))
                    {
                        return RedirectToAction("Index", "Home", new { area = "AreaForUserRoleOne" });
                    }
                    else
                    {
                        if (Roles.IsUserInRole(model.UserName, "UserRoleTwo"))
                        {
                            return RedirectToAction("Index", "Home", new { area = "AreaForUserRoleTwo" });
                        }
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }
于 2012-06-30T07:17:14.757 に答える
0

これはあなたが探しているかもしれないものです:

   [Authorize(Users="Smith, Steve", Roles="Admin, PowerUser")]

Users : アクション メソッドへのアクセスが許可されているユーザー名のカンマ区切りリスト。

Roles : ロール名のカンマ区切りリスト。アクション メソッドにアクセスするには、ユーザーはこれらのロールの少なくとも 1 つに属している必要があります。

http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx

于 2012-06-30T07:27:03.120 に答える