zoukankan      html  css  js  c++  java
  • ASP.net实现单点登录

    一、简单说明:

    单点登录(Single Sign On)简称SSO,是目前比较流行的企业业务整合的解决方案之一。在开发企业门户网站或电子商务系统时,设计一个用户只能在同一个网站进行唯一登录的功能,可以避免一个用户名和密码在多个地址进行登录。

    二、技术要点:

    Cache对象主要用户Web应用程序的缓存,对于每个应用程序都需要创建Cache对象的一个实例,并且只要对应的应用程序域保持活动,该实例便保持有效,有段Cache对象实例的所有信息都需要通过HttpContext对象的Cache属性或Page对象的Cache属性来提供。

    三、代码实现

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Web.Caching;
    using System.Data.SqlClient;

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

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
    int i = this.checkLogin(txtName.Text, txtPwd.Text);
    if (i > 0)
    {
    // 作为唯一标识的str_Key,应该是唯一的,这可根据需要自己设定规则。
    // 做为测试,这里用用户名和密码的组合来做标识;也不进行其它的错误检查。
    // 生成str_Key
    string str_Key = txtName.Text + "_" + txtPwd.Text;
    // 得到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;
    // 首次登录成功
    Response.Write("<h2 style='color:red'>你好,登录成功!");
    }
    else
    {
    // 在 Cache 中存在该用户的记录,表名已经登录过,禁止再次登录
    Response.Write("<h2 style='color:red'>抱歉,您好像已经登录了!");
    return;
    }

    }
    else
    {
    Response.Write("用户名称或密码错误!!!");
    }
    }
    protected void btnCandel_Click(object sender, EventArgs e)
    {
    txtName.Text = "";
    txtPwd.Text = "";
    }
    public int checkLogin(string loginName, string loginPwd)
    {
    SqlConnection con = new SqlConnection("Server=(local);database=db_18;Uid=sa;Pwd=");
    SqlCommand myCommand = new SqlCommand("select count(*) from 系统管理员表 where 用户名称=@loginName and 密码=@loginPwd", con);
    myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NVarChar, 20));
    myCommand.Parameters["@loginName"].Value = loginName;
    myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NVarChar, 20));
    myCommand.Parameters["@loginPwd"].Value = loginPwd;
    myCommand.Connection.Open();
    int i = (int)myCommand.ExecuteScalar();
    myCommand.Connection.Close();
    return i;
    }
    }

     
  • 相关阅读:
    使用OpenSSL自建CA + Nginx配置HTTPS
    Windows安装pycrypto失败记录
    在Scrapy项目【内外】使用scrapy shell命令抓取 某网站首页的初步情况
    计算机类学术论文 28个常见出版社一般写法(参考文献用)
    自然语言处理的CNN模型中几种常见的池化方法
    自然语言处理相关项目列表(2018/02/05更新)
    【原创】python嗅探QQ消息实战
    pynlpir 报错 Cannot Save user dictionary 原因与解决方法
    开发文档之 概要设计说明书 详细设计说明书 数据库设计说明书
    爬取大众点评美食频道店铺信息实践
  • 原文地址:https://www.cnblogs.com/lihaibo/p/2204026.html
Copyright © 2011-2022 走看看