Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用jqueryajax进行表单身份验证_Asp.net_Jquery_Forms Authentication - Fatal编程技术网

Asp.net 使用jqueryajax进行表单身份验证

Asp.net 使用jqueryajax进行表单身份验证,asp.net,jquery,forms-authentication,Asp.net,Jquery,Forms Authentication,我正在尝试使用jquery实现身份验证,以向将对用户进行身份验证的页面方法发出ajax请求。这是我在下面编写的一个基本示例。实际应用程序更复杂,不使用页面方法来处理身份验证。问题在于用户对象中的isAuthenticated属性始终为false。这个项目是用vb完成的,但我不介意答案/代码是否用c# Ajax请求: $.ajax({ type: 'POST', url: 'default.aspx/authenticateUser', data: "{ username:

我正在尝试使用jquery实现身份验证,以向将对用户进行身份验证的页面方法发出ajax请求。这是我在下面编写的一个基本示例。实际应用程序更复杂,不使用页面方法来处理身份验证。问题在于用户对象中的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

公共共享函数authenticateUser(ByVal用户名作为字符串,ByVal密码作为字符串)作为布尔值
如果(已验证(用户名、密码)),则
Dim票证作为新表单身份验证票证(1,用户名,DateTime.Now,DateTime.Now.AddMinutes(3),False,“成员”)
Dim cookie作为新的HttpCookie(FormsAuthentication.FormsCookieName,FormsAuthentication.Encrypt(票证))
HttpContext.Current.Request.Cookies.Add(cookie)
返回真值
如果结束
返回错误
端函数

问题似乎是由于我误解了何时使用HttpContext.Current.Request和HttpContext.Current.Response。简单的错误。我找到了答案。将请求发送到page方法进行验证后,必须使用HttpContext.Current.Response设置cookie,并使用HttpContext.Current.request检索cookie