.NET基于Redis缓存实现单点登陆SSO的解决方案

1、基本概念

最近公司的多个业务系统要统一整合使用同一个登陆,这就是咱们耳熟能详的单点登陆,如今就NET基于Redis缓存实现单点登陆作一个简单的分享。html

单点登陆(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只须要登陆一次就能够访问全部相互信任的应用系统。redis

普通的登陆是写入session,每次获取session看看是否有登陆就可记录用户的登陆状态。缓存

同理多个站点用一个凭证,能够用分布式session,咱们能够用redis实现分布式session,来实现一个简单的统一登陆demo服务器

咱们在本地IIS创建三个站点session

http://www.a.com  登陆验证的站点app

http://test1.a.com 站点1分布式

http://test2.a.com 站点2ui

修改host文件C:\Windows\System32\drivers\etc下url

127.0.0.1 www.a.comspa

127.0.0.1 test1.a.com

127.0.0.1 test2.a.com

127.0.0.1 sso.a.com

具体实现原理,当用户第一次访问应用系统test1的时候,由于尚未登陆,会被引导到认证系统中进行登陆;根据用户提供的登陆信息,认证系统进行身份校验,若是经过校验,应该返回给用户一个认证的凭据--ticket;用户再访问别的应用的时候就会将这个ticket带上,做为本身认证的凭据,应用系统接受到请求以后会把ticket送到认证系统进行校验,检查ticket的合法性。若是经过校验,用户就能够在不用再次登陆的状况下访问应用系统test2和应用系统test3了。

项目结构

2、代码实现

sso.a.com登陆验证站点

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="ADJ.SSO.Web.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>SSO demo</title>
</head>
<body>
    <form id="form1" runat="server">
           用  户:<input id="txtUserName" type="text" name="userName" /><br /><br />
           密  码:<input type="password" name="passWord" /><br /><br />
            <input type="submit" value="登陆" /><br /><br />
         
            <span style="color: red; margin-top: 20px;"><%=StrTip %></span>
    </form>

</body>
</html>

代码:

 public partial class Index : System.Web.UI.Page
    {
        //定义属性
        public string StrTip { get; set; }
        public string UserName { get; set; }
        public string PassWork { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                ValidateUser();
            }
        }

        //登陆验证
        private void ValidateUser()
        {
            var username = Request.Form["userName"];
            if (username.Equals(""))
            {
                StrTip = "请输入用户名";
                return;
            }
            var password = Request.Form["passWord"];
            if (password.Equals(""))
            {
                StrTip = "请输入密码";
                return;
            }

            //模拟登陆
            if (username == "admin" && password == "admin")
            {
                UserInfo userInfo=new UserInfo()
                {
                    UserName = "admin",PassWord = "admin",Info ="登陆模拟" 
                };

                //生成token
                var token = Guid.NewGuid().ToString();
                //写入token
                Common.Common.AddCookie("token", token, Int32.Parse(ConfigurationManager.AppSettings["Timeout"]));
                //写入凭证
                RedisClient client = new RedisClient(ConfigurationManager.AppSettings["RedisServer"], 6379);
                client.Set<UserInfo>(token, userInfo);


                //跳转回分站
                if (Request.QueryString["backurl"] != null)
                {
                    Response.Redirect(Request.QueryString["backurl"].Decrypt(), false);
                }
                else
                {
                    Response.Redirect(ConfigurationManager.AppSettings["DefaultUrl"], false);
                }
            }
            else
            {
                StrTip = "用户名或密码有误!";
                return; 
            }

        }
    }

配置文件:

<appSettings>
  <!--sso验证-->
  <add key="UserAuthUrl" value="http://sso.a.com/"/>
  <!--redis服务器-->
  <add key="RedisServer" value="192.168.10.121"/>
  <!--过时时间-->
  <add key="Timeout" value="30"/>
  <!--默认跳转站点-->
  <add key="DefaultUrl" value="http://test1.a.com/"/>
</appSettings>

注销代码:

var tokenValue = Common.Common.GetCookie("token");
Common.Common.AddCookie("token",tokenValue,-1);
HttpContext.Current.Response.Redirect(ConfigurationManager.AppSettings["DefaultUrl"]);

其余站点验证是否登陆的代码:PassportService

public class PassportService
    {
        public static string TokenReplace()
        {
            string strHost = HttpContext.Current.Request.Url.Host;
            string strPort = HttpContext.Current.Request.Url.Port.ToString();
            string url = String.Format("http://{0}:{1}{2}", strHost, strPort, HttpContext.Current.Request.RawUrl);
            url = Regex.Replace(url, @"(\?|&)Token=.*", "", RegexOptions.IgnoreCase);
            return ConfigurationManager.AppSettings["UserAuthUrl"] + "?backurl=" + url.Encrypt();
        }
        public void Run()
        {
            var token = Common.Common.GetCookie("token");

            RedisClient client = new RedisClient(ConfigurationManager.AppSettings["RedisServer"], 6379);

            UserInfo userInfo = client.Get<UserInfo>(token);
            if (userInfo == null)
            {                                                                                                                        
                Common.Common.AddCookie("token", token, -1);
                //令牌错误,从新登陆
                HttpContext.Current.Response.Redirect(TokenReplace(), false);
            }
            else
            {
                Common.Common.AddCookie("token", token, Int32.Parse(ConfigurationManager.AppSettings["Timeout"]));
            }
        }

        public UserInfo GetUserInfo()
        {
            var token = Common.Common.GetCookie("token");
            RedisClient client = new RedisClient(ConfigurationManager.AppSettings["RedisServer"], 6379);
            return client.Get<UserInfo>(token) ?? new UserInfo();
        }

    }

3、最后看下效果图

4、代码下载

这里只作了一个简单的实现,提供了一个简单的思路,具体用的时候能够继续完善。

代码下载:

http://pan.baidu.com/s/1pK9U8Oj

 

5、加关注

若是本文对你有帮助,请点击右下角【好文要顶】和【关注我

相关文章
相关标签/搜索