mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-18 17:48:01 +08:00
优化sso
This commit is contained in:
@@ -93,7 +93,6 @@
|
||||
<Compile Include="SSO\AppInfoService.cs" />
|
||||
<Compile Include="SSO\AuthUtil.cs" />
|
||||
<Compile Include="SSO\PassportLoginRequest.cs" />
|
||||
<Compile Include="SSO\ServiceContext.cs" />
|
||||
<Compile Include="SSO\SSOAuthUtil.cs" />
|
||||
<Compile Include="SSO\SSOController.cs" />
|
||||
<Compile Include="SSO\LoginResult.cs" />
|
||||
|
@@ -1,9 +1,10 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Helper.Cache;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public class AppInfoService : ServiceContext
|
||||
public class AppInfoService : CacheProvider
|
||||
{
|
||||
public AppInfo Get(string appKey)
|
||||
{
|
||||
|
@@ -1,45 +1,42 @@
|
||||
using System.Web;
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
/// <summary>
|
||||
/// 采用Attribute的方式验证登陆
|
||||
/// <para>李玉宝新增于2016-11-09 10:08:10</para>
|
||||
/// </summary>
|
||||
public class SSOAuthAttribute : ActionFilterAttribute
|
||||
{
|
||||
public const string Token = "Token";
|
||||
public const string SessionUserName = "SessionUserName";
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
||||
{
|
||||
var token = "";
|
||||
var cookieSessionUserName = "";
|
||||
|
||||
//Token by QueryString
|
||||
var request = filterContext.HttpContext.Request;
|
||||
if (request.QueryString[Token] != null)
|
||||
{
|
||||
token = request.QueryString[Token];
|
||||
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(Token, token));
|
||||
var cookie = new HttpCookie(Token, token)
|
||||
{
|
||||
Expires = DateTime.Now.AddDays(1)
|
||||
};
|
||||
filterContext.HttpContext.Response.Cookies.Add(cookie);
|
||||
}
|
||||
else if (request.Cookies[Token] != null) //从Cookie读取Token
|
||||
{
|
||||
token = request.Cookies[Token].Value;
|
||||
}
|
||||
|
||||
//SessionUserName by QueryString
|
||||
if (request.QueryString[SessionUserName] != null)
|
||||
{
|
||||
cookieSessionUserName = request.QueryString[SessionUserName];
|
||||
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(SessionUserName, cookieSessionUserName));
|
||||
}
|
||||
else if (request.Cookies[SessionUserName] != null) //从Cookie读取SessionUserName
|
||||
{
|
||||
cookieSessionUserName = request.Cookies[SessionUserName].Value;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
//直接登录
|
||||
filterContext.Result = LoginResult(cookieSessionUserName);
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -47,21 +44,16 @@ namespace OpenAuth.App.SSO
|
||||
if (AuthUtil.CheckLogin(token, request.RawUrl) == false)
|
||||
{
|
||||
//会话丢失,跳转到登录页面
|
||||
filterContext.Result = LoginResult(cookieSessionUserName);
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
base.OnActionExecuting(filterContext);
|
||||
}
|
||||
|
||||
private static ActionResult LoginResult(string username)
|
||||
public virtual ActionResult LoginResult(string username)
|
||||
{
|
||||
//跳转到SSO站点登陆
|
||||
//return new RedirectResult(string.Format("{0}/sso/login?appkey={1}&username={2}",
|
||||
// ConfigurationManager.AppSettings["SSOPassport"],
|
||||
// ConfigurationManager.AppSettings["SSOAppKey"],
|
||||
// username));
|
||||
|
||||
return new RedirectResult("/Login/Index");
|
||||
}
|
||||
}
|
||||
|
@@ -25,12 +25,10 @@ namespace OpenAuth.App.SSO
|
||||
public class SSOController : Controller
|
||||
{
|
||||
public const string Token = "Token";
|
||||
public const string SessionUserName = "SessionUserName";
|
||||
|
||||
protected override void OnActionExecuting(ActionExecutingContext filterContext)
|
||||
{
|
||||
var token = "";
|
||||
var cookieSessionUserName = "";
|
||||
|
||||
//Token by QueryString
|
||||
var request = filterContext.HttpContext.Request;
|
||||
@@ -48,21 +46,10 @@ namespace OpenAuth.App.SSO
|
||||
token = request.Cookies[Token].Value;
|
||||
}
|
||||
|
||||
//SessionUserName by QueryString
|
||||
if (request.QueryString[SessionUserName] != null)
|
||||
{
|
||||
cookieSessionUserName = request.QueryString[SessionUserName];
|
||||
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(SessionUserName, cookieSessionUserName));
|
||||
}
|
||||
else if (request.Cookies[SessionUserName] != null) //从Cookie读取SessionUserName
|
||||
{
|
||||
cookieSessionUserName = request.Cookies[SessionUserName].Value;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
//直接登录
|
||||
filterContext.Result = LoginResult(cookieSessionUserName);
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
else
|
||||
@@ -71,7 +58,7 @@ namespace OpenAuth.App.SSO
|
||||
if (AuthUtil.CheckLogin(token, request.RawUrl) == false)
|
||||
{
|
||||
//会话丢失,跳转到登录页面
|
||||
filterContext.Result = LoginResult(cookieSessionUserName);
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -79,14 +66,8 @@ namespace OpenAuth.App.SSO
|
||||
base.OnActionExecuting(filterContext);
|
||||
}
|
||||
|
||||
private static ActionResult LoginResult(string username)
|
||||
public virtual ActionResult LoginResult(string username)
|
||||
{
|
||||
//跳转到SSO站点登陆
|
||||
//return new RedirectResult(string.Format("{0}/sso/login?appkey={1}&username={2}",
|
||||
// ConfigurationManager.AppSettings["SSOPassport"],
|
||||
// ConfigurationManager.AppSettings["SSOAppKey"],
|
||||
// username));
|
||||
|
||||
return new RedirectResult("/Login/Index");
|
||||
}
|
||||
}
|
||||
|
@@ -1,64 +0,0 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Infrastructure.Cache;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public abstract class ServiceContext : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存组件
|
||||
/// </summary>
|
||||
public CacheContext CacheContext { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 动态设置缓存对象的新实例
|
||||
/// </summary>
|
||||
/// <param name="cacheContext">缓存实例对象</param>
|
||||
public void SetCacheInstance(CacheContext cacheContext)
|
||||
{
|
||||
//先释放现有的缓存组件
|
||||
if (CacheContext != null)
|
||||
{
|
||||
CacheContext = null;
|
||||
}
|
||||
|
||||
//初始化缓存组件新的实例
|
||||
CacheContext = cacheContext;
|
||||
}
|
||||
|
||||
public void SetCacheInstance(Type cacheContextType)
|
||||
{
|
||||
if (cacheContextType == null)
|
||||
{
|
||||
throw new ArgumentNullException("cacheContextType");
|
||||
}
|
||||
|
||||
if (!typeof(CacheContext).IsAssignableFrom(cacheContextType))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
string.Format(CultureInfo.CurrentCulture, "该类型 {0} 必须继承自抽象类CacheContext", cacheContextType),
|
||||
"cacheContextType");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
CacheContext = Activator.CreateInstance(cacheContextType) as CacheContext;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
String.Format(
|
||||
CultureInfo.CurrentCulture,
|
||||
"创建抽象类 CacheContext 的实例 {0} 失败",
|
||||
cacheContextType),
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,7 +10,8 @@
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using Infrastructure.Cache;
|
||||
using Helper.Cache;
|
||||
using Infrastructure;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
@@ -19,11 +20,11 @@ namespace OpenAuth.App.SSO
|
||||
/// <para>测试环境用的是基于http application的SessionContext</para>
|
||||
/// <para>正式环境可以使用基于memcached的EnyimMemcachedContext</para>
|
||||
/// </summary>
|
||||
public class UserAuthSessionService : ServiceContext
|
||||
public class UserAuthSessionService : CacheProvider
|
||||
{
|
||||
public UserAuthSessionService()
|
||||
{
|
||||
SetCacheInstance(new SessionContext());
|
||||
SetCacheInstance(new HttpApplicationContext());
|
||||
}
|
||||
|
||||
public bool Create(UserAuthSession model)
|
||||
@@ -42,7 +43,9 @@ namespace OpenAuth.App.SSO
|
||||
{
|
||||
var cache = Get(token);
|
||||
if (cache == null) return false;
|
||||
|
||||
LogHelper.Log(token
|
||||
+ "用户:" + cache.UserName
|
||||
+ "登陆有效时间:" + cache.InvalidTime);
|
||||
if (cache.InvalidTime > DateTime.Now)
|
||||
{
|
||||
//延长
|
||||
|
Reference in New Issue
Block a user