0

カスタマイズされたロールベースの認証属性を作成しました。私の考えは、ロール名が「従業員」のユーザーがログインした場合、URL から「管理者」ページにアクセスできないようにすることです。しかし[MyRoleAuthorization]、従業員コントローラーを実装してログインすると、「この Web ページにはリダイレクト ループがあります」というエラーが表示されます。これはコードです[MyRoleAuthorization]

public class MyRoleAuthorization : AuthorizeAttribute
{
    string isAuthorized;
    private string AuthorizeUser(AuthorizationContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext != null)
        {
            var context = filterContext.RequestContext.HttpContext;


            if (Convert.ToString(context.Session["RoleName"]) == "Admin")
            {
                isAuthorized = "Admin";

            }
            else if (Convert.ToString(context.Session["RoleName"]) == "Employee")
            {
                isAuthorized = "Employee";

            }
            else if (Convert.ToString((context.Session["RoleName"])) == "Customer")
            {
                isAuthorized = "Customer";
            }
            else
            {
                throw new ArgumentException("filterContext");
            }
        }
        return isAuthorized;
    }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
            throw new ArgumentException("filterContext");

        if (AuthorizeUser(filterContext) == "Admin")
        {
            filterContext.Result = new RedirectToRouteResult
                 (new RouteValueDictionary(new { controller = "Admin" }));
        }

        else if (AuthorizeUser(filterContext) == "Employee")
        {
            filterContext.Result = new RedirectToRouteResult
                 (new RouteValueDictionary(new { controller = "Employee" }));
        }
        else if (AuthorizeUser(filterContext) == "Customer")
        {
            filterContext.Result = new RedirectToRouteResult
                 (new RouteValueDictionary(new { controller = "Customer" }));

        }
    }

}
} 

私の従業員コントローラは次のようになります

   [MyRoleAuthorization]        
    public ActionResult Index()
    {
        var employee = db.Employee.Include(e => e.User);
        return View(employee.ToList());
    }

手伝ってくれませんか。

4

3 に答える 3

1

最大の問題は、従業員として従業員コントローラーにアクセスすると、従業員コントローラーにリダイレクトされ、属性によって従業員コントローラーにリダイレクトされることです。コードが脆弱になるため、属性内でのリダイレクトは避けてください。数年後に戻ったときに、ルートが意図したとおりに機能しない理由を思い出せません。

これを試して:

public class MyRoleAuthorization : AuthorizeAttribute
{

    public string Role{get;set;}

    private string AuthorizeUser(AuthorizationContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext != null)
        {
            var context = filterContext.RequestContext.HttpContext;

            return (string)context.Session["RoleName"];
        }
        throw new ArgumentException("filterContext");
    }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
            throw new ArgumentException("filterContext");

        var role = AuthorizeUser(filterContext);
        if (role.Equals(Role))
        {
        // insert positive outcome from role check, ie let the action continue
        }
        else
        {
        // denied! redirect to login page or show denied page (403)
        }
    }
} 


[MyRoleAuthorization("Employee")]        
public ActionResult Index()
{
    var employee = db.Employee.Include(e => e.User);
    return View(employee.ToList());
}
于 2014-01-06T02:52:42.747 に答える
0

承認されると、たとえば Customer コントローラーにリダイレクトされるようです。このコントローラーにはおそらくあなたの属性が含まれているため、顧客と見なされるユーザーを承認し、顧客コントローラーにリダイレクトします...属性が含まれているため、ユーザーを承認します...

無限ループ。

于 2014-01-06T02:51:47.763 に答える