簡單使用redis實現sso單點登陸

  前面幾篇分享了nosql只mongodb,今天簡單分享另外一個nosql神兵redis。前端

  主要模仿sso單點登陸,將登陸人信息寫入redis。話很少說,直接上馬,駕。redis

     /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public string Login(string name, string pass)
        {
            //******
            //name,pass登錄成功(^_^)
            //******
            //生成tokan
            var token = "tianshangbaiyujing";//Guid.NewGuid().ToString();
            //*****獲取user信息
            var user = new User();
            user.Name = "白玉京";
            user.Sex = 0;
            user.Age = 20;
            user.Address = "杭州";
            //*****
            //*****將登陸人信息信息redis緩存
            HashEntry[] entrys = new HashEntry[4];//數組對象長度必定要和下面SetValue賦值一一對應
            HashEntry entry = new HashEntry("Name", user.Name);
            entrys.SetValue(entry, 0);
            entry = new HashEntry("Sex", user.Sex);
            entrys.SetValue(entry, 1);
            entry = new HashEntry("Age", user.Age);
            entrys.SetValue(entry, 2);
            entry = new HashEntry("Address", user.Address);
            entrys.SetValue(entry, 3);
            TimeSpan ts = new TimeSpan(864000000000);//默認1天
            RedisHelper.AddHashKey(token, entrys, ts);
            //*****
            //*****將user信息存入userSession對象返回給前端
            var session = new UserSession() { Token = token, User = user };
            //*****
            return JsonConvert.SerializeObject(session);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public string TestUserToken(string token)
        {
            var arr = RedisHelper.GetHashALL(token);
            User user = new User();
            user.Name = arr[0].Value;
            user.Sex = (int)arr[1].Value;
            user.Age = (int)arr[2].Value;
            user.Address = arr[3].Value;
            return JsonConvert.SerializeObject(user);
        }
        /// <summary>
        /// UserSession
        /// </summary>
        public class UserSession
        {
            public string Token { get; set; }
            public User User { get; set; }
        }
        /// <summary>
        /// User
        /// </summary>
        public class User
        {
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            /// 性別0:男,1:女
            /// </summary>
            public int Sex { get; set; }
            /// <summary>
            /// 年齡
            /// </summary>
            public int Age { get; set; }
            /// <summary>
            /// 地址
            /// </summary>
            public string Address { get; set; }
        }

  redisHelper類。sql

      static readonly ConnectionMultiplexer Redis = ConnectionMultiplexer.Connect("127.0.0.1:6379");
        static readonly IDatabase Db = Redis.GetDatabase();

       /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="exp"></param>
        public static void AddHashKey(string key, HashEntry[] hashField, TimeSpan? exp = null)
        {
            Db.HashSet(key, hashField);
            Db.KeyExpire(key, exp);
        }
       /// <summary>
        /// 獲取hash表
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static HashEntry[] GetHashALL(string key)
        {
            return Db.HashGetAll(key);
        }
 

  兩個方法運行結果mongodb

  

  寫了半天,貼兩分鐘完事了,再貼幾張redisServer啓動吧,都是cmd命令,跟mongo很像,就不細說了。數組

  啓動客戶端看下剛纔存的數據。緩存

  這樣登陸完成後,每次請求接口帶上token就能夠獲取登陸信息了。session

相關文章
相關標籤/搜索