[英]IIS7.5 HttpErrors ExecuteURL not executing
我對此完全感到困惑。 基本上,我有一個帶有自定義AuthorizeAttribute的MVC頁面,如果用戶通過了身份驗證,但沒有適當的訪問權限,則該頁面會引發403錯誤。 我遇到的問題是我想將此錯誤重定向到自定義控制器/操作(/ Error / Unauthorized)。
我在我的web.config中添加了以下內容
<httpErrors errorMode="Custom">
<remove statusCode ="403" subStatusCode="-1"/>
<error statusCode="403" path="/Error/Unauthorized" responseMode="ExecuteURL" />
</httpErrors>
通過以上配置,我看不到默認的IIS 7.5 403重定向。 但是,我也看不到任何東西。 在IE中,它告訴我該網站要求您登錄,而chrome只是顯示空白頁面。
有任何想法嗎?
這是自定義授權代碼,以防萬一
public class CustomAuthorize : AuthorizeAttribute
{
//Property to allow array instead of single string.
private string[] _authorizedRoles;
public string[] AuthorizedRoles
{
get { return _authorizedRoles ?? new string[0]; }
set { _authorizedRoles = value; }
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
if (filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.Result = new HttpStatusCodeResult(403);
}
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");
if (!httpContext.User.Identity.IsAuthenticated)
return false;
//Check to see if any of the authorized roles fits into any assigned roles only if roles have been supplied.
if (AuthorizedRoles.Any(httpContext.User.IsInRole))
return true;
return false;
}
}
好的,我不確定這是否正確,但是符合我的症狀。 http://forums.asp.net/t/1462153.aspx/1我對必須對重定向進行編碼感到不滿意,但是我試圖使其至少對於將來的可維護性是顯式的。
public bool RedirectAuthenticatedButUnauthorizedUsers { get; set; }
private String _redirectUnauthorizedUrl = String.Empty;
public String RedirectUnauthorizedUrl
{
get { return _redirectUnauthorizedUrl; }
set { _redirectUnauthorizedUrl = value.Trim(); }
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
if (!RedirectAuthenticatedButUnauthorizedUsers || !filterContext.HttpContext.Request.IsAuthenticated)
return;
if(RedirectUnauthorizedUrl == String.Empty)
throw new NullReferenceException("RedirectAuthenticatedButUnauthorizedUsers " +
"set to true, but no redirect URL set.");
filterContext.HttpContext.Response.Redirect(RedirectUnauthorizedUrl);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.