【发布时间】:2018-11-18 04:04:43
【问题描述】:
我已经设置了一个错误页面,如果抛出 403 错误并且它可以在本地环境中工作,但在部署到 IIS 时它会重定向到默认的 403 禁止页面,即 403 - 禁止访问:拒绝访问。
p>为什么?
代码:
web.config:
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
<globalization uiCulture="en" culture="en-GB"/>
<customErrors mode="Off"></customErrors>
<authentication mode="Forms"></authentication>
<sessionState timeout="60"></sessionState>
</system.web>
C#:
自定义授权:
public class CustomeAuthorize: AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
// just to ensure if the unauthorized request is from an authenticated user or a visitor.
if (!filterContext.HttpContext.User.Identity.IsAuthenticated) // just a visitor since he/she doesn't need any login to proceed
{
//filterContext.Result = new ViewResult { ViewName = "~/Views/Errors/AuthorizeFailedError.cshtml" };
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Errors", action = "AuthorizeFailedError" }));
}
else //authenticated user but authorized to the requested page
{
filterContext.Result = new ViewResult { ViewName = "~/Views/Login/Login.cshtml" };
}
}
【问题讨论】:
标签: c# asp.net-mvc web http-status-code-403