博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Token的管理
阅读量:6317 次
发布时间:2019-06-22

本文共 7695 字,大约阅读时间需要 25 分钟。

代码:

RedisCommon

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using ServiceStack.Redis;using System.Configuration;using ServiceStack.Redis.Generic;using Newtonsoft.Json;namespace Rongzi.BZone.Common.Util{    public class RedisCommon    {        private static readonly Lazy
_instance = new Lazy
(() => new RedisCommon()); private static readonly string redisUrl = ConfigurationManager.AppSettings["Redis_Server"]; private static readonly string redisPort = ConfigurationManager.AppSettings["Redis_Port"]; private RedisCommon() { } public static RedisCommon getInstance { get { return _instance.Value; } } public RedisClient getRedisClient() { return new RedisClient(redisUrl, int.Parse(redisPort)); } #region string类型操作 ///
/// 根据key获取对应的对象T /// ///
///
///
public T GetObj
(string key) { T result; try { using (var redis = this.getRedisClient()) { result = redis.Get
(key); } } catch (Exception) { result = default(T); } return result; } ///
/// 根据key存储T对象 /// ///
///
///
///
///
public bool SetObj
(string key, T val, DateTime dateTime) { bool result = false; try { using (var redis = this.getRedisClient()) { result = redis.Set
(key, val, dateTime); } } catch { result = false; } return result; } ///
/// 根据key更新T /// ///
///
///
///
public bool UpdateObj
(string key, T t) { bool result = false; using (var redis = this.getRedisClient()) { var value = JsonConvert.SerializeObject(t); result = redis.Set
(key, value); } return result; } ///
/// 删除对应key的value /// ///
///
public bool RemoveObj(string key) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.Remove(key); } return result; } #endregion #region hash类型操作 ///
/// 从hash表获取数据 /// public T Get
(string hashId, string key) { using (var redis = this.getRedisClient()) { string value = redis.GetValueFromHash(hashId, key); return JsonConvert.DeserializeObject
(value); } } ///
/// 获取整个hash的数据 /// public List
GetAll
(string hashId) { using (var redis = this.getRedisClient()) { var result = new List
(); var list = redis.GetHashValues(hashId); if (list != null && list.Count > 0) { list.ForEach(x => { var value = JsonConvert.DeserializeObject
(x); result.Add(value); }); } return result; } } ///
/// 判断某个数据是否已经被缓存 /// public bool Exist
(string hashId, string key) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.HashContainsEntry(hashId, key); } return result; } ///
/// 存储数据到hash表 /// public bool Set
(string hashId, string key, T t) { bool result = false; try { using (var redis = this.getRedisClient()) { var value = JsonConvert.SerializeObject(t); result = redis.SetEntryInHash(hashId, key, value); } } catch { result = false; } return result; } ///
/// 移除hash中的某值 /// public bool Remove(string hashId, string key) { bool result = false; try { using (var redis = this.getRedisClient()) { result = redis.RemoveEntryFromHash(hashId, key); } } catch { result = false; } return result; } ///
/// 移除整个hash /// public bool RemoveAll(string hashId) { bool result = false; using (var redis = this.getRedisClient()) { result = redis.Remove(hashId); } return result; } ///
/// 设置缓存过期 /// public void SetExpire(string hashId, DateTime datetime) { using (var redis = this.getRedisClient()) { redis.ExpireEntryAt(hashId, datetime); } } #endregion #region 保存到硬盘 ///
/// 保存数据DB文件到硬盘 /// public void Save() { using (var redis = this.getRedisClient()) { redis.Save(); } } ///
/// 异步保存数据DB文件到硬盘 /// public void SaveAsync() { using (var redis = this.getRedisClient()) { redis.SaveAsync(); } } #endregion }}
View Code

TokenManager

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Newtonsoft.Json;using Rongzi.BZone.Admin.Models;using Rongzi.BZone.Common.Util;using System.Configuration;namespace Rongzi.BZone.Admin.Functions{    public class TokenManager    {        ///         /// 设置对象过期时间        ///         private static readonly int interval = Convert.ToInt32(ConfigurationManager.AppSettings["Redis_TimeInterval"]);        ///         /// 存储对象val,获取对应的token        ///         ///         /// 
public static RedisOpearteResult getToken(Manage_UserInfo val) { string tokenID = Guid.NewGuid().ToString(); RedisOpearteResult result = new RedisOpearteResult { isok = RedisCommon.getInstance.SetObj
(tokenID, val, DateTime.Now.AddMinutes(interval)), token = tokenID, result = JsonConvert.SerializeObject(val) }; return result; } ///
/// 根据tokenID更新用户对象 /// ///
///
///
public static RedisOpearteResult RefreshLoginTokenData(String tokenID, Manage_UserInfo val) { RedisOpearteResult result = new RedisOpearteResult { isok = RedisCommon.getInstance.SetObj
(tokenID, val, DateTime.Now.AddMinutes(interval)), token = tokenID, result = JsonConvert.SerializeObject(val) }; return result; } ///
/// 刷新用户token /// ///
public static RedisOpearteResult RefreshUserToken(string tokenID) { var obj = RedisCommon.getInstance.GetObj
(tokenID); var isExist = obj != null; RedisOpearteResult result = new RedisOpearteResult { isok = isExist, token = tokenID, result = "Token过期" }; if (isExist) { result.result = "成功延迟"; RedisCommon.getInstance.SetObj
(tokenID, obj, DateTime.Now.AddMinutes(interval)); } return result; } ///
/// 退出 /// ///
///
public static RedisOpearteResult LoginOff(string tokenID) { var obj = RedisCommon.getInstance.GetObj
(tokenID); var isExist = obj != null; RedisOpearteResult result = new RedisOpearteResult { isok = isExist, token = tokenID, result = "Token过期" }; if (isExist) { result.result = "退出成功"; RedisCommon.getInstance.RemoveObj(tokenID); } return result; } ///
/// 通过token 获取用户信息 /// ///
tokenID ///
public static bool getUserByToken(string token, out Manage_UserInfo user) { bool isok = false; user = null; if (!string.IsNullOrEmpty(token) && RedisCommon.getInstance.GetObj
(token)!=null) { user = RedisCommon.getInstance.GetObj
(token); isok = true; } return isok; } }}

 

转载地址:http://nxkaa.baihongyu.com/

你可能感兴趣的文章
使用Envoy 作Sidecar Proxy的微服务模式-3.分布式追踪
查看>>
深入了解以太坊
查看>>
SpringBoot 实战 (二) | 第一个 SpringBoot 工程详解
查看>>
Go goroutine理解
查看>>
Java之注解的定义及使用
查看>>
如何进阶一名有竞争力的程序员?
查看>>
聊聊flink的Execution Plan Visualization
查看>>
PAT A1037
查看>>
算法复习
查看>>
IDE 插件新版本发布,开发效率 “biu” 起来了
查看>>
Flutter组件学习(三)—— 输入框TextFiled
查看>>
如何让被遮挡层可以进行事件点击?(纯CSS方法)
查看>>
理解环境变量 JAVA_TOOL_OPTIONS
查看>>
网易云基于Prometheus的微服务监控实践
查看>>
Vant 1.6.15 发布,有赞轻量级移动端 Vue 组件库
查看>>
Kotlin 2018 调查:1/4 的 Java 开发者在迁移后表示后悔
查看>>
社区直播 | NoSQL漫谈:坚持写HBase技术博客的感想
查看>>
ArrayList扩容机制
查看>>
安卓 Chrome 新功能测试中,可以更快切换标签页
查看>>
构造函数、this、static
查看>>