最近老板很变态。。非要用webservice实现单点登录的问题,I 服了。。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;

public partial class web1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string cookieName = "";
            string cookiePassword = "";
            if (Request.Cookies["name"] != null)
            {
                cookieName = Request.Cookies["name"].Value.ToString();
                cookiePassword = Request.Cookies["password"].Value.ToString();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您还没有登陆!')</script>");
            }

            ValidateLoginService v = new ValidateLoginService();
            if (v.DoValidate(cookieName, cookiePassword) == true)
            {
                //表示用户已经登录
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您已经成功登陆!');location.href='MainPager.aspx'</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您还没有登陆!')</script>");
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = this.TextBox1.Text;
        string password = this.TextBox2.Text;
 
        //判断用户名跟密码是否匹配
        ValidateLoginService v = new ValidateLoginService();
        if (v.DoValidate(name, password) == true)
        {
            Response.Cookies["name"].Value = name;
            Response.Cookies["name"].Expires = DateTime.Now.AddDays(1);
            Response.Cookies["name"].Domain = "labyuan.com";

            Response.Cookies["password"].Value = password;
            Response.Cookies["password"].Expires = DateTime.Now.AddDays(1);
            Response.Cookies["password"].Domain = "labyuan.com";

            Session["name"] = name;
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您已经成功登陆!');location.href='MainPager.aspx'</script>");

        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名或密码不正确!')</script>");
        }
    }
}

WebService code

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

/// <summary>
///ValidateLoginService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class ValidateLoginService : System.Web.Services.WebService
{

    public ValidateLoginService()
    {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod(EnableSession = true)]
    public bool DoValidate(string username, string userpwd)
    {
        if (username != null && userpwd != null)
        {
            //判断是否已经有验证通过记录
            if (Session["name"] == null)
            {
                //查找用户名是否存在
                SqlParameter[] para = new SqlParameter[] {
                new SqlParameter("@name",SqlDbType.NVarChar,50),
                new SqlParameter("@password",SqlDbType.NVarChar,50)
            };

                para[0].Value = username;
                para[1].Value = userpwd;
                ExecutionADO exe = new ExecutionADO();

                if (exe.ParameterObject("IsNamePsw", para)!=null)
                {
                    Session["name"] = true;//该用户的验证记录。避免下次再次验证。
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }

}