2012-04-01 12 views
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 

答えて

3

HttpContext.Current.Response対HttpContext.Current.Requestを使用する際の問題は私の誤解によるもので表示されます。簡単な間違い。答えはhereです。検証のためにリクエストがページメソッドに送信されると、HttpContext.Current.Responseを使用してCookieを設定し、HttpContext.Current.Requestで取得する必要がありました。

関連する問題