HttpModule 破坏了 PostBack 事件

发布于 2024-12-01 08:40:17 字数 1742 浏览 4 评论 0原文

我正在尝试设置一个简单的 HttpModule 来处理我的单点登录服务器之间的身份验证。我已经包含了下面模块的代码。该模块正在访问我的 SSO 并正确进行身份验证;然而,在带有表单的页面上,回发事件没有正确发生(例如,即使发生 POST,isPostBack 值也始终为 false,按钮单击事件不会被命中,等等)。

public sealed class MyAuthenticationModule : IHttpModule
{      
    public void Init(HttpApplication context)
    {
        context.AuthenticateRequest += OnAuthenticateRequest;
    }
    public void Dispose()
    {
    }

    public static void OnAuthenticateRequest(object sender, EventArgs e)
    {
        FormsAuthentication.Initialize();

        HttpContext context = HttpContext.Current;
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        // Validate the ticket coming back from the authentication server
        if (!string.IsNullOrEmpty(request["ticket"]))
        {
            // I can include code for this if you want, but it appears to be
            // working correct as whenever I get a ticket from my SSO it is processed
            // correctly. I only get a ticket after coming from the SSO server and
            // then it is removed from the URL so this only gets hit once.
            MyAuthentication.ProcessTicketValidation();
        }

        if (!request.IsAuthenticated)
        {
            // redirect to the login server
            response.Redirect("https://sso.example.com/login.aspx" + "?" + "service=" + 
                                HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri), false);
        }
    }
}

编辑

我还想指出,如果我将行:更改

if (!string.IsNullOrEmpty(request["ticket"]))

为:

if (!string.IsNullOrEmpty(request.QueryString["ticket"]))

问题就会消失。

I'm trying to setup a simple HttpModule to handle authentication between my single sign on server. I've included code for the module below. The module is hitting my SSO and properly authenticating; however, on pages with forms the postback events are not occurring properly (e.g. isPostBack value is always false even though a POST occurred, button click events don't get hit, etc.).

public sealed class MyAuthenticationModule : IHttpModule
{      
    public void Init(HttpApplication context)
    {
        context.AuthenticateRequest += OnAuthenticateRequest;
    }
    public void Dispose()
    {
    }

    public static void OnAuthenticateRequest(object sender, EventArgs e)
    {
        FormsAuthentication.Initialize();

        HttpContext context = HttpContext.Current;
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        // Validate the ticket coming back from the authentication server
        if (!string.IsNullOrEmpty(request["ticket"]))
        {
            // I can include code for this if you want, but it appears to be
            // working correct as whenever I get a ticket from my SSO it is processed
            // correctly. I only get a ticket after coming from the SSO server and
            // then it is removed from the URL so this only gets hit once.
            MyAuthentication.ProcessTicketValidation();
        }

        if (!request.IsAuthenticated)
        {
            // redirect to the login server
            response.Redirect("https://sso.example.com/login.aspx" + "?" + "service=" + 
                                HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri), false);
        }
    }
}

EDIT

I would also like to note that if I change the line:

if (!string.IsNullOrEmpty(request["ticket"]))

to:

if (!string.IsNullOrEmpty(request.QueryString["ticket"]))

the problem goes away.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-12-08 08:40:17

您的回发是否可能有重复的表单数据变量“ticket”?这似乎可以向我解释这种行为。

除此之外,这一行很可疑:

FormsAuthentication.Initialize();

FormsAuthentication 类使用“Provider”模式,这意味着它是一个单例。您不应该重新初始化。 来自 msdn 文档:

FormsAuthenticationModule 时调用 Initialize 方法
创建 FormsAuthentication 类的实例。这个方法是
不打算从您的代码中调用。

Is it possible that your postbacks have a duplicate form data variable, "ticket"? That would seem to explain the behavior to me.

Aside from that, this line is suspicous:

FormsAuthentication.Initialize();

The FormsAuthentication class uses the "Provider" pattern, which means it's a singleton. You should not re-initialize. From the msdn documentation:

The Initialize method is called when the FormsAuthenticationModule
creates an instance of the FormsAuthentication class. This method is
not intended to be called from your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文