0

我在下面有一个登录功能:

    [WebMethod]
    public static bool isAccountAvailable(string userName, string passWord)
    {
        BDTestDataContext bdc = new BDTestDataContext();
        if ((from q in bdc.Accounts where q.User == userName && q.Password == passWord select q).Count() > 0)
            return true;
        else
            return false;
    }

和 jQuery

   $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                url: 'Home.aspx/isAccountAvailable',
                data: JSON.stringify({ userName: $('#txtUserLogin').val(), passWord: $('#txtPassLogin').val() }),
                dataType: 'json',
                success: function (data) {
                    if (data.d == true) {
                        alert("Login success");                         
                    }
                    else
                        alert("Error!");
                }
            });

当我打开另一个页面时,我想创建会话以保存登录状态。例如:/manager.aspx

4

1 回答 1

1

尝试 :

   [WebMethod(Description="isAccountAvailable",EnableSession=true)]
    public static bool isAccountAvailable(string userName, string passWord)
    {
        bool flag = false;
        BDTestDataContext bdc = new BDTestDataContext();
        if ((from q in bdc.Accounts where q.User == userName && q.Password == passWord select q).Count() > 0)
            {
               flag  = true;
            }
             //No need to check else condition bcz flag is already False
            Session["isAccountAvailable"] = flag ;

        return flag
    }

根据操作要求编辑:

 [WebMethod(Description="isAccountAvailable",EnableSession=true)]
    public static string isAccountAvailable(string userName, string passWord)
    {
        bool flag = "";
        string Uname="";
        BDTestDataContext bdc = new BDTestDataContext();
        if ((from q in bdc.Accounts where q.User == userName && q.Password == passWord select q).Count() > 0)
            {
               flag  = true;
               Uname  = userName;
            }
             //No need to check else condition bcz flag is already False
            Session["isAccountAvailable"] = flag ;

        return string ;
    }

并在您的成功功能中:

         success: function (data) {
                if (data.d !="") {
                    alert(data.Uname);                         
                }
                else
                    alert("User Name is Empty");
            }
于 2013-10-25T10:31:31.923 回答