asp.net-mvc - 无法使ASP.NET MVC应用重定向到未经授权的用户的错误页面

标签 asp.net-mvc model-view-controller error-handling web-config

我在ASP.NET MVC应用程序的web.config中进行了以下设置:

<authentication mode="Windows" />
<authorization>
    <allow roles="MySecurityGroup"/>
    <deny users="*"/>
</authorization>
<customErrors mode="On" defaultRedirect="Error.aspx">
    <error statusCode="401" redirect="Help.aspx"/>
</customErrors>
如果您位于MySecurityGroup中,则一切正常,但是如果不在MySecurityGroup中,则不会重定向到Error.aspx或Help.aspx。 (请注意,Error.aspx位于Views \ Shared中,而Help.aspx位于Views \ Home中。)您得到的只是默认错误:

Server Error in '/' Application.

Access is denied.

Description: An error occurred while accessing the resources required to serve this request. The server may not be configured for access to the requested URL.

Error message 401.2.: Unauthorized: Logon failed due to server configuration. Verify that you have permission to view this directory or page based on the credentials you supplied and the authentication methods enabled on the Web server. Contact the Web server's administrator for additional assistance.


我究竟做错了什么?
更新:现在我的web.config像这样设置,但仍然无法正常工作:
<system.web>
    <customErrors mode="On" defaultRedirect="Help.aspx">
    </customErrors>
</system.web>

<location path="">
    <system.web>
        <authorization>
            <allow roles="MySecurityGroup"/>
            <deny users="*"/>
        </authorization>
    </system.web>
</location>

<location path="Help">
    <system.web>
        <authorization>
            <allow users="*"/>
        </authorization>
    </system.web>
</location>
请注意,我可以很好地导航到MyApp/Help,并且已被该网站的其余部分正确禁止,但它从未自动重定向到“帮助”页面。

最佳答案

您必须显式地授予对Error.aspx和/或​​Help.aspx的其他组的访问权限,这样他们才可以实际访问页面。您现在设置的方式只有MySecurityGroup用户可以访问页面。

您将需要以下内容:

<location path="Error.aspx">
 <system.web>
   <authorization>
    <allow users="*"/>
   </authorization>
 </system.web>
</location>

与Help.aspx相同。或者,您可以在文件夹级别执行此操作。

关于asp.net-mvc - 无法使ASP.NET MVC应用重定向到未经授权的用户的错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1002501/

相关文章:

ruby-on-rails - 如何在 Rails 中创建全局可访问的模块?

ruby - Sinatra 中的全局救援和日志记录异常

jquery - Javascript 字符串化对象

c# - 在 ASP.NET MVC 中嵌套异步/等待调用

jquery - 复杂类型的 Ajax 模型绑定(bind)

c# - 如何将 javascript 和 cshtml 文件分散到单独的项目中

ruby-on-rails - 为什么重定向错误?

javascript - 类似 MVC 的代码如何在 Node.js 中工作?

c - c : different approaches 中的库错误管理

error-handling - 无法在 flutter 控制台中执行 flutter 医生命令