2013-05-21 32 views
2

我通過Ajax以Json格式傳遞用戶名和傳遞給.aspx頁面中的webmethod, 現在我想驗證用戶並將用戶重定向到同一頁面並使用LogedIn狀態更新LoginView;如何通過WebMethod進行身份驗證

我該怎麼做? 這裏是我的WebMethod

[WebMethod] 
    // [ [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet=false)] 
    public static void login(object myData) 
    { 

     JavaScriptSerializer js = new JavaScriptSerializer(); 
     List<nameVal> myfrm = js.Deserialize<List<nameVal>>(myData.ToString()); 
     // MembershipUser u = Membership.GetUser(myfrm.SingleOrDefault(rs=>rs.name=="userName").value); 

     if ( Membership.ValidateUser(myfrm[0].value,myfrm[1].value)) 
     { 
      FormsAuthentication.Authenticate(myfrm[0].value, myfrm[1].value); 
      //FormsAuthentication.RedirectFromLoginPage(myfrm[0].value, false); 

      FormsAuthenticationTicket tkt; 
      string cookiestr; 
      HttpCookie ck; 
      tkt = new FormsAuthenticationTicket(1, myfrm[0].value, DateTime.Now, 
      DateTime.Now.AddMinutes(30), true, "my custom data"); 
      cookiestr = FormsAuthentication.Encrypt(tkt); 
      ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr); 

      ck.Path = FormsAuthentication.FormsCookiePath; 
      // ******* now i must somthing like this: --> Response.Cookies.Add(ck); 
      // but im in static method don't have respons request objects 
      // i want respons in json form and proccess the json 


      // return "Success"; 
     } 
     else 
     { 
      //return "Faild"; 
     } 

    } 

所以坦克

+0

有啥問題..設置會話/ cookie,並使用您的重定向頁面上.. –

+0

如何更新LoginView和LogginName控制的國家, – Mohammadreza

+0

如何設置會話或Cookie的 – Mohammadreza

回答

0

你可以做到以下幾點:

FormsAuthentication.SetAuthCookie(myfrm[0].value, createPersistentCookie: false); 
return FormsAuthentication.GetRedirectUrl(user.Name, false); 

,然後在你的JavaScript:

var onSuccess = function (result) { 
    if (result === null) { 
     // login failed 
     return; 
    } 
    window.location.href = result; 
}; 

PageMethods.login(myData, onSuccess); 

這可惜還是有一個「沉默「ThreadAbortException,我似乎無法擺脫,但它似乎並不導致任何問題。我認爲這可能是不可避免的,是良性的。

相關問題