优化SSO登录

This commit is contained in:
yubaolee
2016-12-27 11:25:51 +08:00
parent a3bdcf83ec
commit ccf1269eca
19 changed files with 108 additions and 240 deletions

View File

@@ -98,7 +98,6 @@
<Compile Include="SSO\LoginResult.cs" />
<Compile Include="SSO\SSOAuthAttribute.cs" />
<Compile Include="SSO\UserAuthSession.cs" />
<Compile Include="SSO\UserAuthSessionService.cs" />
<Compile Include="CommonApplyApp.cs" />
<Compile Include="StockManagerApp.cs" />
<Compile Include="UserManagerApp.cs" />

View File

@@ -1,6 +1,6 @@
using System;
using System.Linq;
using Helper.Cache;
using Infrastructure.Cache;
namespace OpenAuth.App.SSO
{

View File

@@ -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
{

View File

@@ -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);
}

View File

@@ -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
};
//<2F><><EFBFBD><EFBFBD>Session
new UserAuthSessionService().Create(currentSession);
new ObjCacheProvider<UserAuthSession>().Create(currentSession.Token, currentSession, DateTime.Now.AddDays(10));
result.Success = true;
result.ReturnUrl = appInfo.ReturnUrl;

View File

@@ -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);
}

View File

@@ -13,8 +13,6 @@ namespace OpenAuth.App.SSO
public string IpAddress { get; set; }
public DateTime InvalidTime { get; set; }
public DateTime CreateTime { get; set; }
}
}

View File

@@ -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
{
/// <summary>
/// 用户登录状态存储服务
/// <para>测试环境用的是基于http application的SessionContext</para>
/// <para>正式环境可以使用基于memcached的EnyimMemcachedContext</para>
/// </summary>
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<UserAuthSession>(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);
}
}
}