diff --git a/Infrastructure/Cache/CacheContext.cs b/Infrastructure/Cache/CacheContext.cs new file mode 100644 index 00000000..f063e6fe --- /dev/null +++ b/Infrastructure/Cache/CacheContext.cs @@ -0,0 +1,44 @@ +// *********************************************************************** +// Assembly : Helper +// Author : yubaolee +// Created : 12-16-2016 +// +// Last Modified By : yubaolee +// Last Modified On : 12-21-2016 +// 使用微软默认带超时的Cache +// File: CacheContext.cs +// *********************************************************************** + +using System; +using System.Web; + +namespace Infrastructure.Cache +{ + public class CacheContext : ICacheContext + { + private readonly System.Web.Caching.Cache _objCache = HttpRuntime.Cache; + public override T Get(string key) + { + System.Web.Caching.Cache objCache = HttpRuntime.Cache; + return (T) objCache[key]; + } + + public override bool Set(string key, T t, DateTime expire) + { + var obj = Get(key); + if (obj != null) + { + Remove(key); + } + + _objCache.Insert(key, t, null, expire, System.Web.Caching.Cache.NoSlidingExpiration); + return true; + } + + public override bool Remove(string key) + { + _objCache.Remove(key); + return true; + } + } +} diff --git a/Infrastructure/Cache/CacheObj.cs b/Infrastructure/Cache/CacheObj.cs deleted file mode 100644 index d1d59ac3..00000000 --- a/Infrastructure/Cache/CacheObj.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace Helper.Cache -{ - [Serializable] - public class CacheObj - { - public string key { get; set; } - - public T Obj { get; set; } - - public DateTime InvalidTime { get; set; } - - public DateTime CreateTime { get; set; } - } -} \ No newline at end of file diff --git a/Infrastructure/Cache/CacheProvider.cs b/Infrastructure/Cache/CacheProvider.cs index 667eff5d..18c0255b 100644 --- a/Infrastructure/Cache/CacheProvider.cs +++ b/Infrastructure/Cache/CacheProvider.cs @@ -1,7 +1,7 @@ using System; using System.Globalization; -namespace Helper.Cache +namespace Infrastructure.Cache { /// /// 缓存工厂 diff --git a/Infrastructure/Cache/EnyimMemcachedContext.cs b/Infrastructure/Cache/EnyimMemcachedContext.cs index 24e407b7..64640dcf 100644 --- a/Infrastructure/Cache/EnyimMemcachedContext.cs +++ b/Infrastructure/Cache/EnyimMemcachedContext.cs @@ -10,40 +10,29 @@ // *********************************************************************** +using System; using Enyim.Caching; using Enyim.Caching.Memcached; -namespace Helper.Cache +namespace Infrastructure.Cache { public sealed class EnyimMemcachedContext : ICacheContext { - private readonly MemcachedClient _memcachedClient = new MemcachedClient("memcached"); - - public override void Init() - { - } + private static readonly MemcachedClient _memcachedClient = new MemcachedClient(); public override T Get(string key) { return _memcachedClient.Get(key); } - public override bool Set(string key, T t) + public override bool Set(string key, T t, DateTime expire) { - return _memcachedClient.Store(StoreMode.Set, key, t); + return _memcachedClient.Store(StoreMode.Set, key, t, expire); } public override bool Remove(string key) { return _memcachedClient.Remove(key); } - - public override void Dispose() - { - if (_memcachedClient != null) - { - _memcachedClient.Dispose(); - } - } } } \ No newline at end of file diff --git a/Infrastructure/Cache/HttpApplicationContext.cs b/Infrastructure/Cache/HttpApplicationContext.cs deleted file mode 100644 index 3913cf5b..00000000 --- a/Infrastructure/Cache/HttpApplicationContext.cs +++ /dev/null @@ -1,65 +0,0 @@ -// *********************************************************************** -// Assembly : Helper -// Author : Administrator -// Created : 09-21-2016 -// -// Last Modified By : Administrator -// Last Modified On : 11-09-2016 -// Contact : -// File: HttpApplicationContext.cs -// *********************************************************************** - - -using System; -using System.Web; - -namespace Helper.Cache -{ - /// - /// 基于HttpApplication的存储 - /// 李玉宝新增于2016-11-09 9:30:51 - /// - public sealed class HttpApplicationContext : ICacheContext - { - - public override void Init() - { - } - - public override T Get(string key) - { - return (T) HttpContext.Current.Application[key]; - } - - public override bool Set(string key, T t) - { - try - { - HttpContext.Current.Application[key] = t; - return true; - } - catch (Exception) - { - return false; - } - } - - public override bool Remove(string key) - { - try - { - HttpContext.Current.Application[key] = null; - return true; - } - catch (Exception) - { - return false; - } - } - - public override void Dispose() - { - - } - } -} \ No newline at end of file diff --git a/Infrastructure/Cache/ICacheContext.cs b/Infrastructure/Cache/ICacheContext.cs index a087c618..cbb3718b 100644 --- a/Infrastructure/Cache/ICacheContext.cs +++ b/Infrastructure/Cache/ICacheContext.cs @@ -1,24 +1,19 @@ using System; -namespace Helper.Cache +namespace Infrastructure.Cache { /// /// 缓存接口 /// - public abstract class ICacheContext : IDisposable + public abstract class ICacheContext { - /// - /// 初始化缓存组件 - /// - public abstract void Init(); - /// /// 获取缓存项 /// /// 缓存对象类型 /// 键 /// 缓存对象 - public abstract T Get(string key) where T : class; + public abstract T Get(string key) ; /// /// 设置缓存项 @@ -27,7 +22,7 @@ namespace Helper.Cache /// 键 /// 缓存对象 /// true成功,false失败 - public abstract bool Set(string key, T t) where T : class; + public abstract bool Set(string key, T t, DateTime expire); /// /// 移除一个缓存项 @@ -36,9 +31,5 @@ namespace Helper.Cache /// true成功,false失败 public abstract bool Remove(string key); - /// - /// 释放缓存组件 - /// - public abstract void Dispose(); } } \ No newline at end of file diff --git a/Infrastructure/Cache/ObjCacheProvider.cs b/Infrastructure/Cache/ObjCacheProvider.cs index 84c52d1c..127158fc 100644 --- a/Infrastructure/Cache/ObjCacheProvider.cs +++ b/Infrastructure/Cache/ObjCacheProvider.cs @@ -1,39 +1,34 @@ // *********************************************************************** -// Assembly : OpenAuth.WebApi -// Author : yubaolee -// Created : 07-11-2016 +// Assembly : Helper +// Author : Administrator +// Created : 12-21-2016 // -// Last Modified By : yubaolee -// Last Modified On : 07-11-2016 +// Last Modified By : Administrator +// Last Modified On : 12-22-2016 // Contact : -// File: CacheObjService.cs +// File: ObjCacheProvider.cs // *********************************************************************** + using System; -namespace Helper.Cache +namespace Infrastructure.Cache { /// - /// 带超时结构的缓存 + /// 缓存工厂实现 + /// 这样做是方便换其他的缓存时(如memcachedContext)只换这一个地方即可 /// public class ObjCacheProvider : CacheProvider { public ObjCacheProvider() { - SetCacheInstance(new HttpApplicationContext()); + SetCacheInstance(new CacheContext()); } - public bool Create(string key, T val) + public bool Create(string key, T val, DateTime expire) { - var cacheobj = new CacheObj - { - key = key, - InvalidTime = DateTime.Now.AddMinutes(5), - CreateTime = DateTime.Now, - Obj = val - }; //设置缓存 - return CacheContext.Set(key, cacheobj); + return CacheContext.Set(key, val, expire); } /// @@ -43,18 +38,7 @@ namespace Helper.Cache /// The key. public T GetCache(string key) { - var cache = CacheContext.Get>(key); - if (cache == null) return default(T); - - if (cache.InvalidTime > DateTime.Now) - { - return cache.Obj; - } - - //移除无效Session缓存 - Remove(key); - - return default(T); + return CacheContext.Get(key); } public void Remove(string key) diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index e63e35da..c82cc7da 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -80,10 +80,9 @@ - + - diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj index de7f4437..834eda2a 100644 --- a/OpenAuth.App/OpenAuth.App.csproj +++ b/OpenAuth.App/OpenAuth.App.csproj @@ -98,7 +98,6 @@ - diff --git a/OpenAuth.App/SSO/AppInfoService.cs b/OpenAuth.App/SSO/AppInfoService.cs index 90974a08..8cb7ce9c 100644 --- a/OpenAuth.App/SSO/AppInfoService.cs +++ b/OpenAuth.App/SSO/AppInfoService.cs @@ -1,6 +1,6 @@ using System; using System.Linq; -using Helper.Cache; +using Infrastructure.Cache; namespace OpenAuth.App.SSO { diff --git a/OpenAuth.App/SSO/AuthUtil.cs b/OpenAuth.App/SSO/AuthUtil.cs index 5b180b8b..c156b5a0 100644 --- a/OpenAuth.App/SSO/AuthUtil.cs +++ b/OpenAuth.App/SSO/AuthUtil.cs @@ -150,7 +150,7 @@ namespace OpenAuth.App.SSO var token = GetToken(); if (String.IsNullOrEmpty(token)) return true; - var requestUri = String.Format("/SSO/Login/Logout?token={0}&requestid={1}", token, ""); + var requestUri = String.Format("/SSO/Check/Logout?token={0}&requestid={1}", token, ""); try { diff --git a/OpenAuth.App/SSO/SSOAuthAttribute.cs b/OpenAuth.App/SSO/SSOAuthAttribute.cs index 3c019a06..9fda7e6b 100644 --- a/OpenAuth.App/SSO/SSOAuthAttribute.cs +++ b/OpenAuth.App/SSO/SSOAuthAttribute.cs @@ -23,7 +23,7 @@ namespace OpenAuth.App.SSO token = request.QueryString[Token]; var cookie = new HttpCookie(Token, token) { - Expires = DateTime.Now.AddDays(1) + Expires = DateTime.Now.AddDays(10) }; filterContext.HttpContext.Response.Cookies.Add(cookie); } diff --git a/OpenAuth.App/SSO/SSOAuthUtil.cs b/OpenAuth.App/SSO/SSOAuthUtil.cs index 1b4ebee9..defba8c6 100644 --- a/OpenAuth.App/SSO/SSOAuthUtil.cs +++ b/OpenAuth.App/SSO/SSOAuthUtil.cs @@ -2,6 +2,7 @@ using System; using System.Web; using System.Web.Mvc; using Infrastructure; +using Infrastructure.Cache; using OpenAuth.Domain; @@ -55,14 +56,13 @@ namespace OpenAuth.App.SSO { UserName = model.UserName, Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"), - InvalidTime = DateTime.Now.AddDays(1), AppKey = model.AppKey, CreateTime = DateTime.Now, IpAddress = HttpContext.Current.Request.UserHostAddress }; //Session - new UserAuthSessionService().Create(currentSession); + new ObjCacheProvider().Create(currentSession.Token, currentSession, DateTime.Now.AddDays(10)); result.Success = true; result.ReturnUrl = appInfo.ReturnUrl; diff --git a/OpenAuth.App/SSO/SSOController.cs b/OpenAuth.App/SSO/SSOController.cs index 376d0007..0911556d 100644 --- a/OpenAuth.App/SSO/SSOController.cs +++ b/OpenAuth.App/SSO/SSOController.cs @@ -37,7 +37,7 @@ namespace OpenAuth.App.SSO token = request.QueryString[Token]; var cookie = new HttpCookie(Token, token) { - Expires = DateTime.Now.AddDays(1) + Expires = DateTime.Now.AddDays(10) }; filterContext.HttpContext.Response.Cookies.Add(cookie); } diff --git a/OpenAuth.App/SSO/UserAuthSession.cs b/OpenAuth.App/SSO/UserAuthSession.cs index fb2a768a..914e176b 100644 --- a/OpenAuth.App/SSO/UserAuthSession.cs +++ b/OpenAuth.App/SSO/UserAuthSession.cs @@ -13,8 +13,6 @@ namespace OpenAuth.App.SSO public string IpAddress { get; set; } - public DateTime InvalidTime { get; set; } - public DateTime CreateTime { get; set; } } } \ No newline at end of file diff --git a/OpenAuth.App/SSO/UserAuthSessionService.cs b/OpenAuth.App/SSO/UserAuthSessionService.cs deleted file mode 100644 index 650246d5..00000000 --- a/OpenAuth.App/SSO/UserAuthSessionService.cs +++ /dev/null @@ -1,70 +0,0 @@ -// *********************************************************************** -// Assembly : OpenAuth.WebApi -// Author : yubaolee -// Created : 07-11-2016 -// -// Last Modified By : yubaolee -// Last Modified On : 07-11-2016 -// Contact : -// File: UserAuthSessionService.cs -// *********************************************************************** - -using System; -using Helper.Cache; -using Infrastructure; - -namespace OpenAuth.App.SSO -{ - /// - /// 用户登录状态存储服务 - /// 测试环境用的是基于http application的SessionContext - /// 正式环境可以使用基于memcached的EnyimMemcachedContext - /// - public class UserAuthSessionService : CacheProvider - { - public UserAuthSessionService() - { - SetCacheInstance(new HttpApplicationContext()); - } - - public bool Create(UserAuthSession model) - { - //设置缓存 - return CacheContext.Set(model.Token, model); - } - - public UserAuthSession Get(string token) - { - var sessionCacheItem = CacheContext.Get(token); - return sessionCacheItem; - } - - public bool GetCache(string token) - { - var cache = Get(token); - if (cache == null) return false; - LogHelper.Log(token - + "用户:" + cache.UserName - + "登陆有效时间:" + cache.InvalidTime); - if (cache.InvalidTime > DateTime.Now) - { - //延长 - cache.InvalidTime = DateTime.Now.AddDays(1); - //设置缓存 - CacheContext.Set(cache.Token, cache); - - return true; - } - - //移除无效Session缓存 - Remove(token); - - return false; - } - - public void Remove(string token) - { - CacheContext.Remove(token); - } - } -} \ No newline at end of file diff --git a/OpenAuth.WebApi/Areas/SSO/Controllers/CheckController.cs b/OpenAuth.WebApi/Areas/SSO/Controllers/CheckController.cs index ad740b82..6c4cae1b 100644 --- a/OpenAuth.WebApi/Areas/SSO/Controllers/CheckController.cs +++ b/OpenAuth.WebApi/Areas/SSO/Controllers/CheckController.cs @@ -9,8 +9,10 @@ // File: CheckController.cs // *********************************************************************** +using System; using System.Web.Mvc; using Infrastructure; +using Infrastructure.Cache; using OpenAuth.App; using OpenAuth.App.SSO; @@ -24,6 +26,7 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers public class CheckController : Controller { private AuthorizeApp _app; + private ObjCacheProvider _objCacheProvider = new ObjCacheProvider(); public CheckController() { _app = AutofacExt.GetFromFac(); @@ -31,7 +34,7 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers public bool GetStatus(string token = "", string requestid = "") { - if (new UserAuthSessionService().GetCache(token)) + if (_objCacheProvider.GetCache(token) != null) { return true; } @@ -52,7 +55,7 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers public string GetUserName(string token, string requestid = "") { - var user = new UserAuthSessionService().Get(token); + var user = _objCacheProvider.GetCache(token); if (user != null) { return user.UserName; @@ -66,5 +69,19 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers { return JsonHelper.Instance.Serialize(SSOAuthUtil.Parse(request)); } + + [HttpPost] + public bool Logout(string token, string requestid) + { + try + { + _objCacheProvider.Remove(token); + return true; + } + catch (Exception) + { + return false; + } + } } } \ No newline at end of file diff --git a/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs b/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs index d84ac0f4..978fc26e 100644 --- a/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs +++ b/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs @@ -51,21 +51,5 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers TempData[AppInfo] = _appInfoService.Get(model.AppKey); return View(model); } - - - - [HttpPost] - public bool Logout(string token, string requestid) - { - try - { - new UserAuthSessionService().Remove(token); - return true; - } - catch (Exception) - { - return false; - } - } } } \ No newline at end of file diff --git a/OpenAuth.WebApi/Web.config b/OpenAuth.WebApi/Web.config index cff68bd2..b9375e36 100644 --- a/OpenAuth.WebApi/Web.config +++ b/OpenAuth.WebApi/Web.config @@ -8,11 +8,25 @@
+ + +
+ + + + + + + + + + +