4
jqueryを使用して認証を実装して、ユーザーを認証するページメソッドに対してajaxリクエストを作成しようとしています。これは私が以下にコーディングした基本的な例です。実際のアプリケーションはより複雑で、ページメソッドを使用して認証を処理しません。問題は、UserオブジェクトのisAuthenticatedプロパティが常にfalseであることです。このプロジェクトはVBで行われますが、回答/コードがC#にあるかどうかは気にしません。jQuery ajaxによるフォーム認証
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