zoukankan      html  css  js  c++  java
  • 用WebService实现登录验证

    红色需修改

    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,加不加也无所谓

  • 相关阅读:
    自动化框架总结-2(转)
    svn离线安装以及配置,管理python自动化脚本
    自动化框架总结-1(转)
    pytest参数化、标记用例、生成html报告
    pytest作为前置和后置的使用
    笔记:常用xpath
    read_ini.py
    深入理解python类装饰器和带参数装饰器
    Python 03-Python3基础语法
    Python 02-Python2.x与3.x版本区别
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1534285.html
Copyright © 2011-2022 走看看