使用SuppressFormsAuthenticationRedirectModule会导致Object Moved Here而不是显示页面内容

时间:2012-09-11 09:57:06

标签: asp.net forms-authentication webforms

我正在使用http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx处的SuppressFormsAuthenticationRedirectModule来阻止重定向某些HTTP 401状态并显示自定义页面。

但是,我将Object Moved Here作为输出。如何显示我的自定义页面?

页面如下

<%@ Page Title="Unauthorized" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeBehind="UnauthorizedError.aspx.vb" Inherits="WebFormsTemplate.UnauthorizedError" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <h2>Unauthorized</h2>
    You do not have permission to view this page.
</asp:Content>

背后的代码

Imports System.Net
Imports Appahoo.Web.WebForms

Public Class UnauthorizedError
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SuppressFormsAuthenticationRedirectModule.SuppressAuthenticationRedirect(Me.Context, False)
        Me.Response.StatusCode = HttpStatusCode.Unauthorized
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

没有意识到SuppressAuthenticationRedirect已经将状态设置为HttpStatusCode.Unauthorized。因此,我应该删除Me.Response.StatusCode = HttpStatusCode.Unauthorized以让页面呈现。

Imports System.Net
Imports Appahoo.Web.WebForms

Public Class UnauthorizedError
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SuppressFormsAuthenticationRedirectModule.SuppressAuthenticationRedirect(Me.Context, False)
    End Sub
End Class