mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-09 02:44:44 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Infrastructure.Cache;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public class AppInfoService : CacheProvider
|
||||
public class AppInfoService
|
||||
{
|
||||
public AppInfo Get(string appKey)
|
||||
{
|
||||
@@ -21,7 +20,7 @@ namespace OpenAuth.App.SSO
|
||||
IsEnable = true,
|
||||
Remark = "基于DDDLite的权限管理系统",
|
||||
ReturnUrl = "http://localhost:56813",
|
||||
Title = "OpenAuth.Net",
|
||||
Title = "OpenAuth.Core",
|
||||
CreateTime = DateTime.Now,
|
||||
},
|
||||
new AppInfo
|
||||
@@ -31,7 +30,7 @@ namespace OpenAuth.App.SSO
|
||||
IsEnable = true,
|
||||
Remark = "这只是个模拟的测试站点",
|
||||
ReturnUrl = "http://localhost:53050",
|
||||
Title = "OpenAuth.Net测试站点",
|
||||
Title = "OpenAuth.Core测试站点",
|
||||
CreateTime = DateTime.Now,
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,182 +0,0 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.App
|
||||
// Author : yubaolee
|
||||
// Created : 07-08-2016
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 07-08-2016
|
||||
// Contact : Microsoft
|
||||
// File: AuthUtil.cs
|
||||
// ***********************************************************************
|
||||
|
||||
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Web;
|
||||
using Infrastructure;
|
||||
using OpenAuth.App.Response;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>վ<EFBFBD><D5BE>¼<EFBFBD><C2BC>֤<EFBFBD><D6A4>
|
||||
/// <para><3E><>¼ʱ<C2BC><CAB1></para>
|
||||
/// <code>
|
||||
/// var result = AuthUtil.Login(AppKey, username, password);
|
||||
/// if (result.Success)
|
||||
/// return Redirect("/home/index?Token=" + result.Token);
|
||||
/// </code>
|
||||
/// </summary>
|
||||
public class AuthUtil
|
||||
{
|
||||
static HttpHelper _helper = new HttpHelper(ConfigurationManager.AppSettings["SSOPassport"]);
|
||||
|
||||
private static string GetToken()
|
||||
{
|
||||
string token = HttpContext.Current.Request.QueryString["Token"];
|
||||
if (!String.IsNullOrEmpty(token)) return token;
|
||||
|
||||
var cookie = HttpContext.Current.Request.Cookies["Token"];
|
||||
return cookie == null ? String.Empty : cookie.Value;
|
||||
}
|
||||
|
||||
public static bool CheckLogin(string token, string remark = "")
|
||||
{
|
||||
if (String.IsNullOrEmpty(token) || String.IsNullOrEmpty(GetToken()))
|
||||
return false;
|
||||
|
||||
var requestUri = String.Format("/api/Check/GetStatus?token={0}&requestid={1}", token, remark);
|
||||
|
||||
try
|
||||
{
|
||||
var value = _helper.Get(null, requestUri);
|
||||
var result = JsonHelper.Instance.Deserialize<Response<bool>>(value);
|
||||
if (result.Code == 200)
|
||||
{
|
||||
return result.Result;
|
||||
}
|
||||
throw new Exception(result.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>¼״̬
|
||||
/// <para>ͨ<><CDA8>URL<52>е<EFBFBD>Token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Cookie<69>е<EFBFBD>Token</para>
|
||||
/// </summary>
|
||||
/// <param name="remark"><3E><>ע<EFBFBD><D7A2>Ϣ</param>
|
||||
public static bool CheckLogin(string remark="")
|
||||
{
|
||||
return CheckLogin(GetToken(), remark);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0>¼<EFBFBD><C2BC><EFBFBD>û<EFBFBD><C3BB><EFBFBD>Ϣ
|
||||
/// <para>ͨ<><CDA8>URL<52>е<EFBFBD>Token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Cookie<69>е<EFBFBD>Token</para>
|
||||
/// </summary>
|
||||
/// <param name="remark">The remark.</param>
|
||||
/// <returns>LoginUserVM.</returns>
|
||||
public static UserWithAccessedCtrls GetCurrentUser(string remark = "")
|
||||
{
|
||||
|
||||
var requestUri = String.Format("/api/Check/GetUser?token={0}&requestid={1}", GetToken(), remark);
|
||||
|
||||
try
|
||||
{
|
||||
var value = _helper.Get(null, requestUri);
|
||||
var result = JsonHelper.Instance.Deserialize<Response<UserWithAccessedCtrls>>(value);
|
||||
if (result.Code == 200)
|
||||
{
|
||||
return result.Result;
|
||||
}
|
||||
throw new Exception(result.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0>¼<EFBFBD><C2BC><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
|
||||
/// <para>ͨ<><CDA8>URL<52>е<EFBFBD>Token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Cookie<69>е<EFBFBD>Token</para>
|
||||
/// </summary>
|
||||
/// <param name="remark">The remark.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public static string GetUserName(string remark = "")
|
||||
{
|
||||
var requestUri = String.Format("/api/Check/GetUserName?token={0}&requestid={1}", GetToken(), remark);
|
||||
|
||||
try
|
||||
{
|
||||
var value = _helper.Get(null, requestUri);
|
||||
var result = JsonHelper.Instance.Deserialize<Response<string>>(value);
|
||||
if (result.Code == 200)
|
||||
{
|
||||
return result.Result;
|
||||
}
|
||||
throw new Exception(result.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>¼<EFBFBD>ӿ<EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="appKey">Ӧ<>ó<EFBFBD><C3B3><EFBFBD>key.</param>
|
||||
/// <param name="username"><3E>û<EFBFBD><C3BB><EFBFBD></param>
|
||||
/// <param name="pwd"><3E><><EFBFBD><EFBFBD></param>
|
||||
/// <returns>System.String.</returns>
|
||||
public static LoginResult Login(string appKey, string username, string pwd)
|
||||
{
|
||||
var requestUri = "/api/Check/Login";
|
||||
|
||||
try
|
||||
{
|
||||
var value = _helper.Post(new
|
||||
{
|
||||
AppKey = appKey,
|
||||
Account = username,
|
||||
Password = pwd
|
||||
}, requestUri);
|
||||
|
||||
var result = JsonHelper.Instance.Deserialize<LoginResult>(value);
|
||||
return result;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ע<><D7A2>
|
||||
/// </summary>
|
||||
public static bool Logout()
|
||||
{
|
||||
var token = GetToken();
|
||||
if (String.IsNullOrEmpty(token)) return true;
|
||||
|
||||
var requestUri = String.Format("/api/Check/Logout?token={0}&requestid={1}", token, "");
|
||||
|
||||
try
|
||||
{
|
||||
var value = _helper.Post(null, requestUri);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
184
OpenAuth.App/SSO/LocalAuth.cs
Normal file
184
OpenAuth.App/SSO/LocalAuth.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using Infrastructure.Cache;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using OpenAuth.App.Interface;
|
||||
using System;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAuth.Repository.Domain;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用本地登录。这个注入IAuth时,只需要OpenAuth.Mvc一个项目即可,无需webapi的支持
|
||||
/// </summary>
|
||||
public class LocalAuth : IAuth
|
||||
{
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
private IOptions<AppSetting> _appConfiguration;
|
||||
private SysLogApp _logApp;
|
||||
|
||||
private AuthContextFactory _app;
|
||||
private LoginParse _loginParse;
|
||||
private ICacheContext _cacheContext;
|
||||
|
||||
public LocalAuth(IHttpContextAccessor httpContextAccessor
|
||||
, AuthContextFactory app
|
||||
, LoginParse loginParse
|
||||
, ICacheContext cacheContext, IOptions<AppSetting> appConfiguration, SysLogApp logApp)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_app = app;
|
||||
_loginParse = loginParse;
|
||||
_cacheContext = cacheContext;
|
||||
_appConfiguration = appConfiguration;
|
||||
_logApp = logApp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 如果是Identity,则返回信息为用户账号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private string GetToken()
|
||||
{
|
||||
if (_appConfiguration.Value.IsIdentityAuth)
|
||||
{
|
||||
return _httpContextAccessor.HttpContext.User.Identity.Name;
|
||||
}
|
||||
string token = _httpContextAccessor.HttpContext.Request.Query[Define.TOKEN_NAME];
|
||||
if (!String.IsNullOrEmpty(token)) return token;
|
||||
|
||||
token = _httpContextAccessor.HttpContext.Request.Headers[Define.TOKEN_NAME];
|
||||
if (!String.IsNullOrEmpty(token)) return token;
|
||||
|
||||
var cookie = _httpContextAccessor.HttpContext.Request.Cookies[Define.TOKEN_NAME];
|
||||
return cookie ?? String.Empty;
|
||||
}
|
||||
|
||||
public bool CheckLogin(string token = "", string otherInfo = "")
|
||||
{
|
||||
if (_appConfiguration.Value.IsIdentityAuth)
|
||||
{
|
||||
return (!string.IsNullOrEmpty(_httpContextAccessor.HttpContext.User.Identity.Name));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
token = GetToken();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = _cacheContext.Get<UserAuthSession>(token) != null;
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前登录的用户信息
|
||||
/// <para>通过URL中的Token参数或Cookie中的Token</para>
|
||||
/// </summary>
|
||||
/// <param name="account">The account.</param>
|
||||
/// <returns>LoginUserVM.</returns>
|
||||
public AuthStrategyContext GetCurrentUser()
|
||||
{
|
||||
if (_appConfiguration.Value.IsIdentityAuth)
|
||||
{
|
||||
return _app.GetAuthStrategyContext(GetToken());
|
||||
}
|
||||
AuthStrategyContext context = null;
|
||||
var user = _cacheContext.Get<UserAuthSession>(GetToken());
|
||||
if (user != null)
|
||||
{
|
||||
context = _app.GetAuthStrategyContext(user.Account);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前登录的用户名
|
||||
/// <para>通过URL中的Token参数或Cookie中的Token</para>
|
||||
/// </summary>
|
||||
/// <param name="otherInfo">The account.</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public string GetUserName(string otherInfo = "")
|
||||
{
|
||||
if (_appConfiguration.Value.IsIdentityAuth)
|
||||
{
|
||||
return _httpContextAccessor.HttpContext.User.Identity.Name;
|
||||
}
|
||||
|
||||
var user = _cacheContext.Get<UserAuthSession>(GetToken());
|
||||
if (user != null)
|
||||
{
|
||||
return user.Account;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录接口
|
||||
/// </summary>
|
||||
/// <param name="appKey">应用程序key.</param>
|
||||
/// <param name="username">用户名</param>
|
||||
/// <param name="pwd">密码</param>
|
||||
/// <returns>System.String.</returns>
|
||||
public LoginResult Login(string appKey, string username, string pwd)
|
||||
{
|
||||
if (_appConfiguration.Value.IsIdentityAuth)
|
||||
{
|
||||
return new LoginResult
|
||||
{
|
||||
Code = 500,
|
||||
Message = "接口启动了OAuth认证,暂时不能使用该方式登录"
|
||||
};
|
||||
}
|
||||
|
||||
var result = _loginParse.Do(new PassportLoginRequest
|
||||
{
|
||||
AppKey = appKey,
|
||||
Account = username,
|
||||
Password = pwd
|
||||
});
|
||||
|
||||
var log = new SysLog
|
||||
{
|
||||
Content = $"用户登录,结果:{result.Message}",
|
||||
Result = result.Code == 200 ? 0 : 1,
|
||||
CreateId = username,
|
||||
CreateName = username,
|
||||
TypeName = "登录日志"
|
||||
};
|
||||
_logApp.Add(log);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注销,如果是Identity登录,需要在controller处理注销逻辑
|
||||
/// </summary>
|
||||
public bool Logout()
|
||||
{
|
||||
var token = GetToken();
|
||||
if (String.IsNullOrEmpty(token)) return true;
|
||||
|
||||
try
|
||||
{
|
||||
_cacheContext.Remove(token);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,77 +1,92 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Infrastructure.Cache;
|
||||
using OpenAuth.Repository.Domain;
|
||||
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public class SSOAuthUtil
|
||||
{
|
||||
public static LoginResult Parse(PassportLoginRequest model)
|
||||
{
|
||||
var result = new LoginResult();
|
||||
try
|
||||
{
|
||||
model.Trim();
|
||||
//获取应用信息
|
||||
var appInfo = new AppInfoService().Get(model.AppKey);
|
||||
if (appInfo == null)
|
||||
{
|
||||
throw new Exception("应用不存在");
|
||||
}
|
||||
//获取用户信息
|
||||
User userInfo = null;
|
||||
if (model.Account == "System")
|
||||
{
|
||||
userInfo = new User
|
||||
{
|
||||
Id = Guid.Empty.ToString(), //TODO:可以根据需要调整
|
||||
Account = "System",
|
||||
Name ="超级管理员",
|
||||
Password = "123456"
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
var usermanager = (UserManagerApp)DependencyResolver.Current.GetService(typeof(UserManagerApp));
|
||||
userInfo = usermanager.Get(model.Account);
|
||||
}
|
||||
|
||||
if (userInfo == null)
|
||||
{
|
||||
throw new Exception("用户不存在");
|
||||
}
|
||||
if (userInfo.Password != model.Password)
|
||||
{
|
||||
throw new Exception("密码错误");
|
||||
}
|
||||
|
||||
var currentSession = new UserAuthSession
|
||||
{
|
||||
Account = model.Account,
|
||||
Name = userInfo.Name,
|
||||
Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"),
|
||||
AppKey = model.AppKey,
|
||||
CreateTime = DateTime.Now,
|
||||
IpAddress = HttpContext.Current.Request.UserHostAddress
|
||||
};
|
||||
|
||||
//创建Session
|
||||
new ObjCacheProvider<UserAuthSession>().Create(currentSession.Token, currentSession, DateTime.Now.AddDays(10));
|
||||
|
||||
result.Code = 200;
|
||||
result.ReturnUrl = appInfo.ReturnUrl;
|
||||
result.Token = currentSession.Token;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Code = 500;
|
||||
result.Message = ex.Message;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 登录解析
|
||||
* 处理登录逻辑,验证客户段提交的账号密码,保存登录信息
|
||||
*/
|
||||
using System;
|
||||
using Infrastructure.Cache;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public class LoginParse
|
||||
{
|
||||
|
||||
//这个地方使用IRepository<User> 而不使用UserManagerApp是防止循环依赖
|
||||
public IRepository<User> _app;
|
||||
private ICacheContext _cacheContext;
|
||||
private AppInfoService _appInfoService;
|
||||
|
||||
public LoginParse( AppInfoService infoService, ICacheContext cacheContext, IRepository<User> userApp)
|
||||
{
|
||||
_appInfoService = infoService;
|
||||
_cacheContext = cacheContext;
|
||||
_app = userApp;
|
||||
}
|
||||
|
||||
public LoginResult Do(PassportLoginRequest model)
|
||||
{
|
||||
var result = new LoginResult();
|
||||
try
|
||||
{
|
||||
model.Trim();
|
||||
//获取应用信息
|
||||
var appInfo = _appInfoService.Get(model.AppKey);
|
||||
if (appInfo == null)
|
||||
{
|
||||
throw new Exception("应用不存在");
|
||||
}
|
||||
//获取用户信息
|
||||
User userInfo = null;
|
||||
if (model.Account == Define.SYSTEM_USERNAME)
|
||||
{
|
||||
userInfo = new User
|
||||
{
|
||||
Id = Guid.Empty.ToString(),
|
||||
Account = Define.SYSTEM_USERNAME,
|
||||
Name ="超级管理员",
|
||||
Password = Define.SYSTEM_USERPWD
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
userInfo = _app.FindSingle(u =>u.Account == model.Account);
|
||||
}
|
||||
|
||||
if (userInfo == null)
|
||||
{
|
||||
throw new Exception("用户不存在");
|
||||
}
|
||||
if (userInfo.Password != model.Password)
|
||||
{
|
||||
throw new Exception("密码错误");
|
||||
}
|
||||
|
||||
var currentSession = new UserAuthSession
|
||||
{
|
||||
Account = model.Account,
|
||||
Name = userInfo.Name,
|
||||
Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"),
|
||||
AppKey = model.AppKey,
|
||||
CreateTime = DateTime.Now
|
||||
// , IpAddress = HttpContext.Current.Request.UserHostAddress
|
||||
};
|
||||
|
||||
//创建Session
|
||||
_cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10));
|
||||
|
||||
result.Code = 200;
|
||||
result.ReturnUrl = appInfo.ReturnUrl;
|
||||
result.Token = currentSession.Token;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Code = 500;
|
||||
result.Message = ex.Message;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
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 override void OnActionExecuting(ActionExecutingContext filterContext)
|
||||
{
|
||||
var token = "";
|
||||
|
||||
//Token by QueryString
|
||||
var request = filterContext.HttpContext.Request;
|
||||
if (request.QueryString[Token] != null)
|
||||
{
|
||||
token = request.QueryString[Token];
|
||||
var cookie = new HttpCookie(Token, token)
|
||||
{
|
||||
Expires = DateTime.Now.AddDays(10)
|
||||
};
|
||||
filterContext.HttpContext.Response.Cookies.Add(cookie);
|
||||
}
|
||||
else if (request.Cookies[Token] != null) //从Cookie读取Token
|
||||
{
|
||||
token = request.Cookies[Token].Value;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
//直接登录
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
//验证
|
||||
if (AuthUtil.CheckLogin(token, request.RawUrl) == false)
|
||||
{
|
||||
//会话丢失,跳转到登录页面
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
base.OnActionExecuting(filterContext);
|
||||
}
|
||||
|
||||
public virtual ActionResult LoginResult(string username)
|
||||
{
|
||||
return new RedirectResult("/Login/Index");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.Mvc
|
||||
// Author : Administrator
|
||||
// Created : 09-22-2015
|
||||
//
|
||||
// Last Modified By : Administrator
|
||||
// Last Modified On : 09-22-2015
|
||||
// ***********************************************************************
|
||||
// <copyright file="BaseController.cs" company="">
|
||||
// Copyright (c) . All rights reserved.
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// 基础控制器
|
||||
// 继承该控制器可以防止未登录查看
|
||||
// 继承该控制器后,如果想访问控制器中存在,但模块配置里面没有的Action(如:Home/Git),请使用AnonymousAttribute
|
||||
// </summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public class SSOController : Controller
|
||||
{
|
||||
public const string Token = "Token";
|
||||
|
||||
protected override void OnActionExecuting(ActionExecutingContext filterContext)
|
||||
{
|
||||
var token = "";
|
||||
|
||||
//Token by QueryString
|
||||
var request = filterContext.HttpContext.Request;
|
||||
if (request.Cookies[Token] != null) //从Cookie读取Token
|
||||
{
|
||||
token = request.Cookies[Token].Value;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
//直接登录
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
//验证
|
||||
if (AuthUtil.CheckLogin(token, request.RawUrl) == false)
|
||||
{
|
||||
//会话丢失,跳转到登录页面
|
||||
filterContext.Result = LoginResult("");
|
||||
return;
|
||||
}
|
||||
|
||||
base.OnActionExecuting(filterContext);
|
||||
}
|
||||
|
||||
public virtual ActionResult LoginResult(string username)
|
||||
{
|
||||
return new RedirectResult("/Login/Index");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
[Serializable]
|
||||
public class UserAuthSession
|
||||
{
|
||||
public string Token { get; set; }
|
||||
|
||||
public string AppKey { get; set; }
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
[Serializable]
|
||||
public class UserAuthSession
|
||||
{
|
||||
public string Token { get; set; }
|
||||
|
||||
public string AppKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户账号
|
||||
/// </summary>
|
||||
public string Account { get; set; }
|
||||
|
||||
/// </summary>
|
||||
public string Account { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
public string IpAddress { get; set; }
|
||||
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
public string IpAddress { get; set; }
|
||||
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user