练习 单点登录(sso)

先看表结构

 

ie 8登录展示

 

ie8 登录成功后界面

 

同样端口打开火狐

 

ie浏览器注销后,火狐才能登录

 

用户登录后产生的数据标识,用于识别授权用户,可为多种方式,DEMO中主站我使用的是Cache,分站使用Session。

 

登录页面 代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ssoLogin.aspx.cs" Inherits="jquerytest.test2.ssoLogin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            账号
            <input type="text" value="" id="txtName" runat="server" />        
        </div>
         <div>
            密码
            <input type="text" value="" id="txtPwd" runat="server" />        
        </div>

        <asp:Button ID="Button1" runat="server" Text="登陆" οnclick="Button1_Click"  />
        
    

    </div>
    
    </form>
</body>
</html>


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Caching;

namespace jquerytest.test2
{
    public partial class ssoLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int i = this.checkLogin(txtName.Value, txtPwd.Value);
            if (i > 0)
            {
                // 作为唯一标识的str_Key,应该是唯一的,这可根据需要自己设定规则。
                // 做为测试,这里用用户名和密码的组合来做标识;也不进行其它的错误检查。 
                // 生成str_Key
                string str_Key = txtName.Value + "_" + txtPwd.Value;
                // 得到Cache中的给定str_Key的值
                string str_User = Convert.ToString(Cache[str_Key]);
                // Cache中没有该str_Key的项目,表名用户没有登录,或者已经登录超时
                if (str_User == String.Empty)
                {
                    // TimeSpan构造函数,用来重载版本的方法,进行判断是否登录。
                    TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
                    HttpContext.Current.Cache.Insert(str_Key, str_Key, null, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, null);
                    Session["User"] = str_Key;
                    // 首次登录成功
                    ShowMsg("你好,登录成功!", "ssoIndex.aspx", 0);
                }
                else
                {
                    // 在 Cache 中存在该用户的记录,表名已经登录过,禁止再次登录
                    Response.Write("<h2 style='color:red'>抱歉,您已经登录了!");
                    return;
                }

            }
            else
            {
                Response.Write("用户名称或密码错误!!!");
            }
        }

        public int checkLogin(string loginName, string loginPwd)
        {
            SqlConnection con = new SqlConnection("Server=(local);database=testDB1;Uid=sa;Pwd=sasa");
            SqlCommand myCommand = new SqlCommand("select count(*) from UserInfo where username=@loginName and pwd=@loginPwd", con);
            myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NChar, 10));
            myCommand.Parameters["@loginName"].Value = loginName;
            myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NChar, 10));
            myCommand.Parameters["@loginPwd"].Value = loginPwd;
            myCommand.Connection.Open();
            int i = (int)myCommand.ExecuteScalar();
            myCommand.Connection.Close();
            return i;
        }



        #region 消息提示  
        /// <summary>
        /// 消息提示
        /// </summary>
        /// <param name="strMsg">消息</param>
        public void ShowMsg(string strMsg)
        {
            string script = "<script>setTimeout(function(){ alert('" + strMsg + "');},1000)</script>";
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), script);

        }

        /// <summary>
        /// 消息提示(跳转页面)
        /// </summary>
        /// <param name="strMsg">消息</param>
        /// <param name="jumpNum">跳转参数</param>
        public void ShowMsg(string strMsg, string page, int jumpNum)
        {
            string script = "";
            if (jumpNum == 0)
                script = "<script>setTimeout(function(){ alert('" + strMsg + "');window.location.href='" + page + "';},1000)</script>";
            else
                script = "<script>setTimeout(function(){ alert('" + strMsg + "');parent.window.location='" + page + "';parent.closeEditor();},1000)</script>";
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), script);

        }

        /// <summary>
        /// 无消息提示(跳转页面)
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="jumpNum">跳转参数</param>
        public void ShowPage(string page, int jumpNum)
        {
            string script = "";
            if (jumpNum == 0)
                script = "<script>setTimeout(function(){ window.location.href='" + page + "';},1000)</script>";
            else
                script = "<script>setTimeout(function(){ parent.window.location='" + page + "';parent.closeEditor();},1000)</script>";
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), script);
        }

        #endregion
    }
}


主页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ssoIndex.aspx.cs" Inherits="jquerytest.test2.ssoIndex" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1 style="color:Red">主页面</h1>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
         <asp:Button ID="btncancel" runat="server" Text="注销" οnclick="btncancel_Click"   />
    </div>
    </form>
</body>
</html>


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jquerytest.test2
{
    public partial class ssoIndex : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                string strUserName = Session["User"].ToString().Split('_')[0];
                Label1.Text = "欢迎光临:"+strUserName;
            }
        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            string str_key = Session["User"] == null ? "" : Session["User"].ToString();
            if (Cache[str_key] != null)
                Cache.Remove(str_key);

            ShowPage("ssoLogin.aspx",0);


        }

        /// <summary>
        /// 无消息提示(跳转页面)
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="jumpNum">跳转参数</param>
        public void ShowPage(string page, int jumpNum)
        {
            string script = "";
            if (jumpNum == 0)
                script = "<script>setTimeout(function(){ window.location.href='" + page + "';},1000)</script>";
            else
                script = "<script>setTimeout(function(){ parent.window.location='" + page + "';parent.closeEditor();},1000)</script>";
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), script);
        }
    }
}


本人参考了 我is小黑  写的博客,细化了局部功能。

对应sso 参考博客地址:http://www.cnblogs.com/wenanry/archive/2009/08/06/1540777.html 试了下作者的 demo 报错,

不过他给出的图 值得大家学习。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值