C# 将未经授权的用户重定向到自定义页面错误

C# 将未经授权的用户重定向到自定义页面错误,c#,asp.net,C#,Asp.net,我试图将未经授权的用户在访问管理员页面时重定向到自定义页面,但我遇到了错误 管理员文件夹的Web.Config <?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="Administrators" /> <deny users="*"/> </authorization

我试图将未经授权的用户在访问管理员页面时重定向到自定义页面,但我遇到了错误

管理员文件夹的Web.Config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <authorization>
      <allow roles="Administrators" />
      <deny users="*"/>
    </authorization>
  </system.web>
</configuration>
以普通用户身份登录并访问管理员页面后出错:

Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>


Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>
“/”应用程序中出现服务器错误。 运行时错误 描述:服务器上发生应用程序错误。此应用程序的当前自定义错误设置阻止远程查看应用程序错误的详细信息(出于安全原因)。但是,本地服务器上运行的浏览器可以查看它。 详细信息:要在远程计算机上查看此特定错误消息的详细信息,请在位于当前web应用程序根目录中的“web.config”配置文件中创建一个标记。然后,该标记的“mode”属性应设置为“Off”。 注意:通过修改应用程序配置标记的“defaultRedirect”属性以指向自定义错误页URL,可以将当前看到的错误页替换为自定义错误页。 删除
并添加

  • -匿名用户
  • *
    -所有用户
  • 并从
    Page\u Load
    事件中删除代码

    如果用户未登录,则将自动重定向到login.aspx。查看root
    web.config
    部分

    <authentication mode="Forms">
            <forms loginUrl ="mylogin.aspx"/> <!-- You can change the url -->
    </authentication>
    

    您可以通过在Global.asax.cs的Application_EndRequest事件中添加以下代码来操作“401 Access Denied”响应的内容(如果是这种情况):

    protected void Application_EndRequest(Object sender, 
                                                 EventArgs e)
      { 
         HttpContext context = HttpContext.Current;
         if (context.Response.Status.Substring(0,3).Equals("401"))
         {
            context.Response.ClearContent();
            context.Response.Write("<script language="javascript">" + 
                         "self.location='../login.aspx';</script>");
         } 
      }
    
    受保护的无效应用程序\u EndRequest(对象发送方、,
    事件参数(e)
    { 
    HttpContext=HttpContext.Current;
    if(context.Response.Status.Substring(0,3).Equals(“401”))
    {
    context.Response.ClearContent();
    context.Response.Write(““+
    “self.location='../login.aspx';”;
    } 
    }
    

    当浏览器识别401并且没有凭据时,客户端重定向将发生。浏览器将显示自定义401页。

    您将收到一般错误消息。打开自定义错误并将正确的错误消息附加到您的帖子。我们无法猜测错误,您需要禁用自定义错误。在
    下添加此配置
    ,并告知具体错误。允许调试也是一个好主意。
    我这样做了,得到了相同的错误*表示所有用户都正确吗?是,但我想将未经授权重定向到某个页面,而不是登录页面
    protected void Application_EndRequest(Object sender, 
                                                 EventArgs e)
      { 
         HttpContext context = HttpContext.Current;
         if (context.Response.Status.Substring(0,3).Equals("401"))
         {
            context.Response.ClearContent();
            context.Response.Write("<script language="javascript">" + 
                         "self.location='../login.aspx';</script>");
         } 
      }