修改部分文件结构,完善第三方登陆功能

This commit is contained in:
yubaolee
2016-07-12 12:28:54 +08:00
parent 994ab81ebb
commit 5f08a59f27
45 changed files with 10078 additions and 246 deletions

View File

@@ -1,9 +1,7 @@
using System;
using System.Linq;
using System.Linq;
using System.Web;
using Infrastructure;
using OpenAuth.App.ViewModel;
using System.Web.Security;
using OpenAuth.App.SSO;
using OpenAuth.Domain.Service;

View File

@@ -79,11 +79,17 @@
<Compile Include="ResourceManagerApp.cs" />
<Compile Include="RevelanceManagerApp.cs" />
<Compile Include="RoleManagerApp.cs" />
<Compile Include="SSO\AppInfo.cs" />
<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" />
<Compile Include="SSO\SSOAuthAttribute.cs" />
<Compile Include="SSO\UserAuthSession.cs" />
<Compile Include="SSO\UserAuthSessionService.cs" />
<Compile Include="StockManagerApp.cs" />
<Compile Include="UserManagerApp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -0,0 +1,37 @@
// ***********************************************************************
// Assembly : SmartSSO
// Author : yubaolee
// Created : 06-08-2016
//
// Last Modified By : yubaolee
// Last Modified On : 07-06-2016
// Contact :
// File: AppInfo.cs
// ***********************************************************************
using System;
namespace OpenAuth.App.SSO
{
/// <summary>
/// 应用程序信息
/// </summary>
public class AppInfo
{
public string AppKey { get; set; }
public string AppSecret { get; set; }
public string Title { get; set; }
public string Remark { get; set; }
public string Icon { get; set; }
public string ReturnUrl { get; set; }
public bool IsEnable { get; set; }
public DateTime CreateTime { get; set; }
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Linq;
namespace OpenAuth.App.SSO
{
public class AppInfoService : ServiceContext
{
public AppInfo Get(string appKey)
{
//可以从数据库读取
return _applist.SingleOrDefault(u => u.AppKey == appKey);
}
private AppInfo[] _applist = new[]
{
new AppInfo
{
AppKey = "openauth",
Icon = "/Areas/SSO/Content/images/logo.png",
IsEnable = true,
Remark = "基于DDDLite的权限管理系统",
ReturnUrl = "http://localhost:56813",
Title = "OpenAuth.Net",
CreateTime = DateTime.Now,
},
new AppInfo
{
AppKey = "openauthtest",
Icon = "/Areas/SSO/Content/images/logo.png",
IsEnable = true,
Remark = "这只是个模拟的测试站点",
ReturnUrl = "http://localhost:53050",
Title = "OpenAuth.Net测试站点",
CreateTime = DateTime.Now,
}
};
}
}

View File

@@ -18,6 +18,15 @@ using OpenAuth.App.ViewModel;
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"]);
@@ -25,23 +34,23 @@ namespace OpenAuth.App.SSO
private static string GetToken()
{
string token = HttpContext.Current.Request.QueryString["Token"];
if (!string.IsNullOrEmpty(token)) return token;
if (!String.IsNullOrEmpty(token)) return token;
var cookie = HttpContext.Current.Request.Cookies["Token"];
return cookie == null ? string.Empty : cookie.Value;
return cookie == null ? String.Empty : cookie.Value;
}
public static bool CheckLogin(string token, string remark = "")
{
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(GetToken()))
if (String.IsNullOrEmpty(token) || String.IsNullOrEmpty(GetToken()))
return false;
var requestUri = string.Format("/SSO/Check/GetStatus?token={0}&requestid={1}", token, remark);
var requestUri = String.Format("/SSO/Check/GetStatus?token={0}&requestid={1}", token, remark);
try
{
var value = _helper.Get(null, requestUri);
return bool.Parse(value);
return Boolean.Parse(value);
}
catch (Exception ex)
{
@@ -49,15 +58,26 @@ namespace OpenAuth.App.SSO
}
}
/// <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 LoginUserVM GetCurrentUser(string remark = "")
{
var requestUri = string.Format("/SSO/Check/GetUser?token={0}&requestid={1}", GetToken(), remark);
var requestUri = String.Format("/SSO/Check/GetUser?token={0}&requestid={1}", GetToken(), remark);
try
{
@@ -79,7 +99,7 @@ namespace OpenAuth.App.SSO
/// <returns>System.String.</returns>
public static LoginResult Login(string appKey, string username, string pwd)
{
var requestUri = "/SSO/Login/Check";
var requestUri = "/SSO/Check/Login";
try
{
@@ -106,9 +126,9 @@ namespace OpenAuth.App.SSO
public static bool Logout()
{
var token = GetToken();
if (string.IsNullOrEmpty(token)) return true;
if (String.IsNullOrEmpty(token)) return true;
var requestUri = string.Format("/SSO/Login/Logout?token={0}&requestid={1}", token, "");
var requestUri = String.Format("/SSO/Login/Logout?token={0}&requestid={1}", token, "");
try
{

View File

@@ -0,0 +1,19 @@
namespace OpenAuth.App.SSO
{
public class PassportLoginRequest
{
public string UserName { get; set; }
public string Password { get; set; }
public string AppKey { get; set; }
public void Trim()
{
UserName = UserName.Trim();
Password = Password.Trim();
if(!string.IsNullOrEmpty(AppKey)) AppKey = AppKey.Trim();
}
}
}

View File

@@ -0,0 +1,62 @@
using System;
using System.Web;
using System.Web.Mvc;
using Infrastructure;
namespace OpenAuth.App.SSO
{
public class SSOAuthUtil
{
public static LoginResult Parse(PassportLoginRequest model)
{
model.Trim();
var result = new LoginResult();
try
{
//<2F><>ȡӦ<C8A1><D3A6><EFBFBD><EFBFBD>Ϣ
var appInfo = new AppInfoService().Get(model.AppKey);
if (appInfo == null)
{
throw new Exception(<>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>");
}
//<2F><>ȡ<EFBFBD>û<EFBFBD><C3BB><EFBFBD>Ϣ
var usermanager = (UserManagerApp) DependencyResolver.Current.GetService(typeof (UserManagerApp));
var userInfo = usermanager.Get(model.UserName);
if (userInfo == null)
{
throw new Exception("<22>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
if (userInfo.Password != model.Password)
{
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
var currentSession = new UserAuthSession
{
UserName = model.UserName,
Token = Guid.NewGuid().ToString().ToMd5(),
InvalidTime = DateTime.Now.AddMinutes(10),
AppKey = model.AppKey,
CreateTime = DateTime.Now,
IpAddress = HttpContext.Current.Request.UserHostAddress
};
//<2F><><EFBFBD><EFBFBD>Session
new UserAuthSessionService().Create(currentSession);
result.Success = true;
result.ReturnUrl = appInfo.ReturnUrl;
result.Token = currentSession.Token;
}
catch (Exception ex)
{
result.Success = false;
result.ErrorMsg = ex.Message;
}
return result;
}
}
}

View File

@@ -0,0 +1,64 @@
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()
{
}
}
}

View File

@@ -1,6 +1,6 @@
using System;
namespace OpenAuth.WebApi.Areas.SSO.Models
namespace OpenAuth.App.SSO
{
[Serializable]
public class UserAuthSession

View File

@@ -0,0 +1,67 @@
// ***********************************************************************
// 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 Infrastructure.Cache;
namespace OpenAuth.App.SSO
{
/// <summary>
/// 用户登录状态存储服务
/// <para>测试环境用的是基于http application的SessionContext</para>
/// <para>正式环境可以使用基于memcached的EnyimMemcachedContext</para>
/// </summary>
public class UserAuthSessionService : ServiceContext
{
public UserAuthSessionService()
{
SetCacheInstance(new SessionContext());
}
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;
if (cache.InvalidTime > DateTime.Now)
{
//延长
cache.InvalidTime = DateTime.Now.AddMinutes(5);
//设置缓存
CacheContext.Set(cache.Token, cache);
return true;
}
//移除无效Session缓存
Remove(token);
return false;
}
public void Remove(string token)
{
CacheContext.Remove(token);
}
}
}