asp.net – 使用jQuery ajax进行表单身份验证

我正在尝试使用 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

解决方法

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....