6

私の MVC アプリケーションでは、HttpUnauthorizedResult クラスを呼び出し、statusDescription パラメーターを指定します。

if (!canAdd) {
    return new HttpUnauthorizedResult("You do not have access to add");
}

これにより、AccountController の Login メソッドもリダイレクトされ、適切な画面にリダイレクトされます。

public ActionResult Login(string returnUrl)
{
if (WebSecurity.IsAuthenticated)
{
    return RedirectToAction("AccessDenied");
}
    ViewBag.ReturnUrl = returnUrl;
    return View();
}

私の質問は、Status Descripton パラメーターをどのように活用するかということです。これらの詳細を AccessDenied ビューに表示するとよいでしょう。

4

1 に答える 1

1

同じ問題が発生しました。Viewbag を設定することはできないため、TempData を使用してメッセージを設定しました。

filterContext.Controller.TempData["message"] = "Access Denied";
filterContext.Result = new HttpUnauthorizedResult();

HttpUnauthorizedResult は、(エラー) メッセージをチェックするアカウント コントローラーのログイン アクションにリダイレクトします。

if (TempData.Count > 0)
{
    var message = TempData["message"];
    ModelState.AddModelError("", message.ToString());
}
于 2013-12-06T11:00:42.587 に答える