Asp.net 在登录页面上添加自定义内容

Asp.net 在登录页面上添加自定义内容,asp.net,forms-authentication,Asp.net,Forms Authentication,我的大多数ASP.NET网站都是匿名web用户可以访问的。但是,在允许访问之前,我需要验证一些页面。我通过web.config控制这一点: <authorization> <allow users="*"/> </authorization> 目前,我的logon.aspx文件是通用的,但我确实希望包含一些说明,告诉用户为什么他被重定向到登录页面。比如: 在您自愿完成任务之前,请先登录,以便系统能够识别您 或 您试图编辑一个事件。系统仅允许管理员

我的大多数ASP.NET网站都是匿名web用户可以访问的。但是,在允许访问之前,我需要验证一些页面。我通过web.config控制这一点:

<authorization>
    <allow users="*"/>
</authorization>

目前,我的logon.aspx文件是通用的,但我确实希望包含一些说明,告诉用户为什么他被重定向到登录页面。比如:

在您自愿完成任务之前,请先登录,以便系统能够识别您

您试图编辑一个事件。系统仅允许管理员执行此操作。请登录,以便我们可以验证您是否是此事件的管理员

登录页面上的说明取决于用户在表单身份验证重定向之前尝试的操作

我的问题是登录页面如何确定采取了什么操作?是否仍要将自定义查询字符串传递到登录页?我想我可以解码ReturnUrl并使用它来尝试确定要显示的指令。然而,这种方法让人觉得……肮脏。我不喜欢登录页面依赖于系统中其他页面的URL名称

有什么建议吗?

您可以使用来了解用户的意图

或者,您可以在重定向之前设置/变量,并使用它显示一些消息。

您可以使用来了解用户的意图


或者,您可以在重定向之前设置/变量,并使用它来显示一些消息。

为什么不使用自定义异常来执行此操作呢

public class ClassName: Exception
    {
        protected ClassName() : base() {}
        public ClassName(String message) : base(message) { }
    }
那你呢

public class First: ClassName
{
    public First() : base("message") {}
}

public class Second: ClassName
{
    public Second() : base("message") { }
}

现在,您只需捕获ClassName类型的异常,并获取消息值并将其传递到asp标签或文本框中,或者以您希望的方式传递它,为什么不使用自定义异常来执行此操作呢

public class ClassName: Exception
    {
        protected ClassName() : base() {}
        public ClassName(String message) : base(message) { }
    }
那你呢

public class First: ClassName
{
    public First() : base("message") {}
}

public class Second: ClassName
{
    public Second() : base("message") { }
}

现在,您只需捕获ClassName类型的异常,并获取消息值并将其传递到asp标签或文本框中,或者按照您的意愿进行传递

您还可以在global.asax的Application.EndRequest中捕获重定向

void Application_EndRequest(object sender, EventArgs e)
    {
        if (!HttpContext.Current.Request.IsAuthenticated && HttpContext.Current.Response.StatusCode == 302 && HttpContext.Current.Response.RedirectLocation != null && HttpContext.Current.Response.RedirectLocation.ToLower().Contains("login.aspx"))
        {
            // do some switching or determining here, or have items preset in your HttpContext.Current.Items[]
            HttpContext.Current.Response.RedirectLocation += "&Action=blah";

        }
    }

您还可以在global.asax的Application.EndRequest中捕获重定向

void Application_EndRequest(object sender, EventArgs e)
    {
        if (!HttpContext.Current.Request.IsAuthenticated && HttpContext.Current.Response.StatusCode == 302 && HttpContext.Current.Response.RedirectLocation != null && HttpContext.Current.Response.RedirectLocation.ToLower().Contains("login.aspx"))
        {
            // do some switching or determining here, or have items preset in your HttpContext.Current.Items[]
            HttpContext.Current.Response.RedirectLocation += "&Action=blah";

        }
    }

异常如何跨HTTP重定向传播?异常如何跨HTTP重定向传播?