web-dev-qa-db-ja.com

ASP.Net MVC3許可されていないユーザーをloginUrlにリダイレクトしない

aSP.Net MVC3を使用し、役割にメンバーシップを使用しているプロジェクトがあります。私はすべてのコントローラーで承認を使用します。例えば:

[Authorize(Roles = "Administrator")]
    public ActionResult Index(string q, int i)
    {
      return View(model);
    }

誰かが管理者の役割を持っていない場合、デフォルトでログインページにリダイレクトされます。それを変更する方法、それでそれはViews/Shared/UnAuthorize.cshtmlにリダイレクトされますか?または、誰かが管理者の役割を持っていない場合は、メッセージボックス(アラート)が表示されますか?

前もって感謝します。

17
ntep vodka

Web.configに表示する必要のあるページを変更するだけです(ルートが存在することを確認してください)

<authentication mode="Forms">
  <forms loginUrl="~/UnAuthorize" timeout="2880" />
</authentication>

代わりに、すべてのロールの特定のパスにリダイレクトする場合は、独自のパスでAuthorizeAttributeを拡張できます。このようなもの(テストされていません、私はあなたにアイデアを与えるためにこれを書きます)

public class CheckAuthorize : ActionFilterAttribute
{
  public Roles[] Roles { get; set; }
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    //Your code to get the user
    var user = ((ControllerBase)filterContext.Controller).GetUser();

    if (user != null)
    {
      foreach (Role role in Roles)
      {
        if (role == user.Role)
          return;
      }
    }      
    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
    if user.Role==Role.Administrator
    {
      redirectTargetDictionary.Add("action", "Unauthorized");
      redirectTargetDictionary.Add("controller", "Home");
    }
    else
    {
      redirectTargetDictionary.Add("action", "Logon");
      redirectTargetDictionary.Add("controller", "Home");
    }
    filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
  }
}
11
Iridio

私は自分の問題を解決しました。私はこれをするだけです:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

public class MyAuthorize : AuthorizeAttribute
{
   protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
   {
     //you can change to any controller or html page.
     filterContext.Result = new RedirectResult("/cpanel/roles/unauthorize");

   }
 }

myAuthorizeをクラスまたはアクションに適用します。

[MyAuthorize]
public class AdminController :Controller
{
}

それでおしまい。

24
ntep vodka

さて、あなたはAuthorizeAttributeから継承し、認証されていない/認証されていないリクエストのリダイレクトを担当するHandleUnauthorizedRequestをオーバーライドすることができます。私は思う この質問 あなたに役立つでしょう

以下のコードが役に立ちました。これがstackoverflowのリファレンスです ASP.NET MVC 4カスタム認証属性-許可されていないユーザーをエラーページにリダイレクトする方法は?

public class CustomAuthorize: AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
        else
        {
            filterContext.Result = new RedirectToRouteResult(new
            RouteValueDictionary(new{ controller = "Error", action = "AccessDenied" }));
        }
    }
}
1
vineel

Ntepウォッカに基づいた私自身のバージョン:

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if(IsUserAuthenticated(filterContext.HttpContext)) 
        {
            filterContext.Result = new RedirectResult("/Account/InvalidRole");
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
    }

    private bool IsUserAuthenticated(HttpContextBase context)
    {
        return context.User != null && context.User.Identity != null && context.User.Identity.IsAuthenticated;
    }
}

このようにして、認証されていないユーザーのログインページへの標準リダイレクトと、認証されているがアクションに適切な役割を持たないユーザーのカスタムリダイレクトを取得します。

1
Konamiman

私はこの方法を使用しており、実装は非常に簡単です。

Asp.net MVC3の保護

Global.asaxのログオンページへのデフォルトルートを変更します

0
Orhan Cinar