IT

红色需修改

asmx.cs

[WebMethod]
    public bool LoginCheck(string ServerAddr,string ID,string PWD)
    {
        bool flag = false;
        string str = "Data Source=" + ServerAddr + ";Initial Catalog=TEST;User ID=sa;Password=XXXX";
        SqlConnection logincon = new SqlConnection(str);
        SqlCommand logincmd = new SqlCommand("select * from TABLE where ID=@ID and PWD=@PWD", logincon);
        logincmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
        logincmd.Parameters.Add("@PWD", SqlDbType.VarChar).Value = PWD;
        SqlDataAdapter loginsda = new SqlDataAdapter(logincmd);
        DataSet loginds = new DataSet();
        loginsda.Fill(loginds);
        if (loginds.Tables[0].Rows.Count > 0)
        {
            Session.Add("user", loginds);
            flag = true;
        }
        return flag;
    }

login.aspx的随便搞两个textbox就可以了,把参数传给webservice

login.aspx.cs(登录事件)

LoginService WsLogin=new LoginService(); //LoginService是定义的webservice名称
            if (WsLogin.LoginCheck(DropDownList1.SelectedItem.Value.Split(':')[0],tb_ID.Text,tb_PWD.Text))
            {
                HttpCookie cookie = new HttpCookie("Info");//定义cookie对象以及名为Info的项
                //DateTime dt = DateTime.Now;//定义时间对象
                //TimeSpan ts = new TimeSpan(30, 0, 0, 0);//cookie有效作用时间,四个参数分别是日时分秒

                //dt.Add(ts);//添加作用时间               

                //上面两句是定义cookie的时间,当前用的是永久有效

                cookie.Expires = DateTime.MaxValue;//永久有效

                cookie.Values.Add("emp_num", tb_ID.Text);//增加属性,需要其他属性自己加
                Response.AppendCookie(cookie);//确定写入cookie中
                Response.Redirect("main.aspx");
            }
            else
                Response.Write("<script>alert('请检查IDPWD');</script>");

 

登录进去后可以正常用webservice添加的session,建议cookie保存的时候加个密,当然我现在只存个ID,加不加也无所谓

posted on 2009-07-29 16:58  liufei  阅读(1628)  评论(0编辑  收藏  举报