单点登录 ---密钥

1、需求描述html

如今有A系统和B系统,须要在A系统进行单点登录到B系统。ide

2、B系统要作事post

一、提供一个可让A系统登录的网址测试

http://localhost:8083/Account/SingleSignOn/?u=xxx&token=FB92B341DBDB59D7ui

其中,u为加密后的用户名,token为B系统与A系统单点登陆握手密钥加密

二、控制器中的方法代码以下spa

[AllowAnonymous]
        [NoTransaction]
        /// <summary>
        /// 执行单点登陆
        /// </summary>
        public ActionResult SingleSignOn()
        {
            RedirectToRouteResult result = null;
            string usertokenString = Request.Params["token"];
            string userName = Request.Params["u"];
            string loginname = DESHelper.Decrypt(userName);
            string token = System.Configuration.ConfigurationSettings.AppSettings["token"];
            if (!string.IsNullOrEmpty(usertokenString))
            {
                if (usertokenString != token || string.IsNullOrEmpty(userName))
                {
                    result = RedirectToAction("NotRole", "Home");
                }
                else
                {
                    MembershipUser user = Membership.GetUser(loginname, false);
                    if (user == null)
                    {
                        //用户不存在
                        result = RedirectToAction("UserNotExist", "Home");
                    }
                    else if (user.IsLockedOut)
                    {
                        //用户被锁定
                        result = RedirectToAction("UserIsLockedOut", "Home");
                    }
                    else if (!user.IsApproved)
                    {
                        //用户未启用 
                        result = RedirectToAction("UserIsApproved", "Home");
                    }
                    else
                    {
                        LogInfoService lis = new LogInfoService();
                        LogInfo lilist = new LogInfo(ProfileHelper.GetName(), "登陆", "登陆");
                        lis.Save(lilist);
                        result = RedirectToAction("Index", "Member");
                    }
                }
            }
            else
                result = RedirectToAction("NotRole", "Home");

            Session["userid"] = loginname;
            System.Web.Security.FormsAuthentication.SetAuthCookie(loginname, true);
            return result;
        }

 

首先咱们要接过传过来的token和用户名u,code

其次咱们要对用户名进行解密orm

最后咱们要比较token是否是A系统认证的token,并查找用户名是否存在htm

若是都知足则让用户登录

三、解密用户名,要步骤2中,要对传过来的用户名进行解密,加密解决的方法都提供了

 

/// <summary>
    /// DES对称加密
    /// </summary>
    public static class DESHelper
    {
        /// <summary>
        /// 根据用户名解密
        /// </summary>
        /// <param name="val"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static string Decrypt(string val, string userid = "")
        {

            var key = GetKey(userid);
            var iv = GetDefaultIV();
            return Decrypt(val, key, iv);
        }

        /// <summary>
        /// 根据用户名加密
        /// </summary>
        /// <param name="val"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static string Encrypt(string val, string userid = "")
        {

            var key = GetKey(userid);
            var iv = GetDefaultIV();
            return Encrypt(val, key, iv);
        }

        /// <summary>
        /// Des加密方法
        /// </summary>
        /// <param name="val"></param>
        /// <param name="key"></param>
        /// <param name="IV"></param>
        /// <returns></returns>
        public static string Encrypt(string val, string key, string IV, CipherMode cipherMode = CipherMode.ECB)
        {
            try
            {
                if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(key))
                {
                    throw new Exception("密钥和偏移向量不足8位");
                }

                if (key.Length > 8) key = key.Substring(0, 8);
                if (IV.Length > 8) IV = IV.Substring(0, 8);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] btKey = Encoding.Default.GetBytes(key);
                byte[] btIV = Encoding.Default.GetBytes(IV);
                StringBuilder builder = new StringBuilder();
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Encoding.Default.GetBytes(val);
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);
                        cs.FlushFinalBlock();
                    }

                    foreach (byte num in ms.ToArray())
                    {
                        builder.AppendFormat("{0:X2}", num);
                    }
                } 
                return builder.ToString();
            }
            catch // (Exception ex)
            {
                return "";
            }
        }

        /// <summary>
        /// Des解密方法
        /// cbc模式:(key值和iv值一致)
        /// </summary>
        /// <param name="val"></param>
        /// <param name="key"></param>
        /// <param name="IV"></param>
        /// <returns></returns>
        public static string Decrypt(string val, string key, string IV, CipherMode cipherMode = CipherMode.ECB)
        {
            try
            {
                if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(key))
                {
                    throw new Exception("密钥和偏移向量不足8位");
                }

                if (key.Length > 8) key = key.Substring(0, 8);
                if (IV.Length > 8) IV = IV.Substring(0, 8);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] btKey = Encoding.Default.GetBytes(key);
                byte[] btIV = Encoding.Default.GetBytes(IV);
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = new byte[val.Length / 2];
                    for (int i = 0; i < (val.Length / 2); i++)
                    {
                        int num2 = Convert.ToInt32(val.Substring(i * 2, 2), 0x10);
                        inData[i] = (byte)num2;
                    }

                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);
                        cs.FlushFinalBlock();
                    }

                    return Encoding.Default.GetString(ms.ToArray());
                }
            }
            catch // (System.Exception ex)
            {
                return "";
            }
        }

        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5(string str)
        {
            if (string.IsNullOrEmpty(str)) return str;
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            string encoded = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", "");
            return encoded;
        }

        private static string GetKey(string key)
        {
            string defaultKey = "C560F02F693B4A0BA62D2B3B5FB74534";
            string sTemp = defaultKey;
            if (!string.IsNullOrEmpty(key))
            {
                sTemp = string.Concat(key, defaultKey.Substring(key.Length, 32 - key.Length));
            }
            return MD5(sTemp).Substring(0, 8);
        }
        /// <summary>   
        /// 得到初始向量IV 
        /// </summary>   
        /// <returns>初试向量IV</returns>   
        private static string GetDefaultIV()
        {
            string sTemp = "87DE696F56DE49C0B96EB85139A48805";
            return MD5(sTemp).Substring(0, 8);
        }
    }

 

 

 

 

3、A系统要作的事

一、直接访问B提供的网址

http://localhost:8083/Account/SingleSignOn/?token=FB92B341DBDB59D7&u="+userName

其中用户名userName是进行加密了的

二、加密用户名的方法

public string GetUserId()
        {
           return Encrypt(HttpContext.Current.User.Identity.Name);
        }

        public string Encrypt(string val)
        {
            var key = "87DE696F";//测试期间请用此key使用
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] btKey = Encoding.Default.GetBytes(key);
                byte[] btIV = Encoding.Default.GetBytes(key);
                StringBuilder builder = new StringBuilder();
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Encoding.Default.GetBytes(val);
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);
                        cs.FlushFinalBlock();
                    }

                    foreach (byte num in ms.ToArray())
                    {
                        builder.AppendFormat("{0:x2}", num);
                    }
                }
                return builder.ToString();
            }
            catch // (Exception ex)
            {
                return "";
            }
        }