簡體   English   中英

asp.net mvc授權屬性重定向

[英]asp.net mvc authorize attribute redirect

在我的應用程序中,我已經制作了這樣的自定義屬性

public class AdminAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var isAuthorized = base.AuthorizeCore(httpContext);
        if (!isAuthorized || Auth.CurrentAdminUser == null)
        {
            return false;
        }
        else
        {
            return (SuperAdmin.Get(Auth.CurrentAdminUser.Id) != null) ? true : false;
        }
    }
}

它工作正常,但我想要的是根據用戶是否未登錄進行重定向,然后登錄頁面,如果用戶已登錄但不是超級管理員,請將其取消授權頁面。

現在發生的是所有未經授權的東西都通過web.config文件重定向到這個頁面,

<authentication mode="Forms">
  <forms loginUrl="~/Site/NotAuthorize" timeout="2880" />
  <!-- this is where we can set up that if you are not authenticated, where should you go then?-->
</authentication> 

任何幫助將非常感激。

您應該覆蓋HandleUnauthorizedRequest

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
    base.HandleUnauthorizedRequest(filterContext);

    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "xxx", action = "xxx", area = "" }));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM