【问题标题】:HttpModule is breaking PostBack eventsHttpModule 正在破坏 PostBack 事件
【发布时间】:2011-08-25 20:51:35
【问题描述】:

我正在尝试设置一个简单的 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"]))

问题消失了。

【问题讨论】:

    标签: asp.net authentication forms-authentication httpmodule


    【解决方案1】:

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

    除此之外,这条线很可疑:

    FormsAuthentication.Initialize();

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

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

    【讨论】:

    • 感谢您的提示。这并没有解决问题,但我还是会继续删除它。
    • 修改了我的答案以解决实际问题...见第一行??我想这就是我所拥有的一切......
    • 好主意,但不幸的是这也不是问题。
    • 这让我想到,这是否会导致问题,因为在页面生命周期中发生与回发相关的事情之前访问表单数据集合?
    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 2021-01-10
    • 2021-08-07
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多