【问题标题】:Forms authentication with jQuery ajax使用 jQuery ajax 进行表单身份验证
【发布时间】:2012-04-01 18:05:48
【问题描述】:

我正在尝试使用 jquery 实现身份验证,以向将验证用户身份的页面方法发出 ajax 请求。这是我在下面编写的一个基本示例。实际应用程序更复杂,并且不使用页面方法来处理身份验证。问题是 User 对象中的 isAuthenticated 属性始终为 false。这个项目是在 vb 中完成的,但我不介意答案/代码是否在 c# 中。

Ajax 请求:

$.ajax({
    type: 'POST',
    url: 'default.aspx/authenticateUser',
    data: "{ username: '" + username + "', password: '" + password + "' }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (d) {
        if (d.d == true) {
            window.location.href = '/registrants/home.aspx';
        }            
    }
});

页面方法:

<WebMethod()>
Public Shared Function authenticateUser(ByVal username As String, ByVal password As String) As Boolean
    If (isAuthenticated(username, password)) Then
        Dim ticket As New FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(3), False, "member")
        Dim cookie As New HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket))
        HttpContext.Current.Request.Cookies.Add(cookie)
        Return True
    End If
    Return False
End Function

【问题讨论】:

    标签: asp.net jquery forms-authentication


    【解决方案1】:

    看来问题是由于我对何时使用 HttpContext.Current.Request 与 HttpContext.Current.Response 有误解。简单的错误。我找到了答案here。一旦请求被发送到页面方法进行验证,cookie 必须使用 HttpContext.Current.Response 设置并使用 HttpContext.Current.Request 检索。

    【讨论】:

      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2010-09-28
      • 1970-01-01
      • 2011-07-11
      相关资源
      最近更新 更多