mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2026-06-29 02:21:50 +08:00
增加身份认证支持缓存可分布式,增加异常处理及登录身份认证。
This commit is contained in:
20
Infrastructure/Auth/CacheKey.cs
Normal file
20
Infrastructure/Auth/CacheKey.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Infrastructure.Auth
|
||||
{
|
||||
public class CacheKey
|
||||
{
|
||||
public static string SessionName = "OpenAuth";
|
||||
public static string UserSessionName = "Session_";
|
||||
private static string GetSessionId()
|
||||
{
|
||||
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(SessionName);
|
||||
string remoteBrowserIp = WebUtility.GetIP();
|
||||
return UserSessionName + remoteBrowserIp + ":" + cookie.Value;
|
||||
}
|
||||
public static string UserID
|
||||
{
|
||||
get { return GetSessionId(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
79
Infrastructure/Auth/CacheSession.cs
Normal file
79
Infrastructure/Auth/CacheSession.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Infrastructure.Cache;
|
||||
using System;
|
||||
using System.Web;
|
||||
|
||||
namespace Infrastructure.Auth
|
||||
{
|
||||
public class CacheSession
|
||||
{
|
||||
ICache cache = DIContainer.Resolve<ICache>();
|
||||
private HttpContext context;
|
||||
public CacheSession(bool IsReadOnly)
|
||||
{
|
||||
this.IsReadOnly = IsReadOnly;
|
||||
}
|
||||
public CacheSession(HttpContext context, bool IsReadOnly, TimeSpan TimeOut, ICache cacheService)
|
||||
{
|
||||
this.context = context;
|
||||
this.IsReadOnly = IsReadOnly;
|
||||
this.TimeOut = TimeOut;
|
||||
}
|
||||
public CacheSession(HttpContext context, bool IsReadOnly)
|
||||
{
|
||||
this.context = context;
|
||||
this.IsReadOnly = IsReadOnly;
|
||||
GetSessionId();
|
||||
if (CacheKey.UserID != null)
|
||||
{
|
||||
var userInfo = cache.Get<dynamic>(CacheKey.UserID);
|
||||
}
|
||||
}
|
||||
//获取会话是否已经验证通过
|
||||
public bool IsAuthenticated
|
||||
{
|
||||
get
|
||||
{
|
||||
if (cache.Get(SessionId) == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//会话唯一Id
|
||||
public string SessionId
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetSessionId();
|
||||
}
|
||||
}
|
||||
public static string SessionName = CacheKey.SessionName;
|
||||
public static string UserSessionName = CacheKey.UserSessionName;
|
||||
//指示会话是否为只读,true为只读
|
||||
public bool IsReadOnly { get; set; }
|
||||
//超时期限
|
||||
public TimeSpan TimeOut { get; set; }
|
||||
private string GetSessionId()
|
||||
{
|
||||
HttpCookie cookie = context.Request.Cookies.Get(SessionName);
|
||||
string remoteBrowserIp = WebUtility.GetIP();
|
||||
if (cookie == null || string.IsNullOrEmpty(cookie.Value))
|
||||
{
|
||||
string newSessionId = Guid.NewGuid().ToString();
|
||||
HttpCookie newCookie = new HttpCookie(SessionName, newSessionId);
|
||||
newCookie.HttpOnly = IsReadOnly;
|
||||
context.Response.Cookies.Add(newCookie);
|
||||
return UserSessionName + remoteBrowserIp + ":" + newSessionId;
|
||||
}
|
||||
else
|
||||
{
|
||||
return UserSessionName + remoteBrowserIp + ":" + cookie.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Infrastructure/Auth/FormsAuthenticationService.cs
Normal file
48
Infrastructure/Auth/FormsAuthenticationService.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Infrastructure.Cache;
|
||||
using System;
|
||||
using System.Web;
|
||||
|
||||
namespace Infrastructure.Auth
|
||||
{
|
||||
/// <summary>
|
||||
/// 身份认证服务实现(缓存可分布式部署)
|
||||
/// </summary>
|
||||
public class FormsAuthenticationService : IAuthenticationService
|
||||
{
|
||||
ICache cacheService;
|
||||
CacheSession cacheSession;
|
||||
HttpContext httpContext = HttpContext.Current;
|
||||
//hpf 缓存相关
|
||||
public FormsAuthenticationService()
|
||||
{
|
||||
cacheService = DIContainer.Resolve<ICache>();
|
||||
cacheSession = new CacheSession(httpContext, true);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前认证的用户
|
||||
/// </summary>
|
||||
/// <returns>当前用户未通过认证则返回null</returns>
|
||||
public dynamic GetAuthenticatedUser()
|
||||
{
|
||||
if (httpContext == null || !cacheSession.IsAuthenticated)
|
||||
{
|
||||
return null;//hpf未登录
|
||||
}
|
||||
return cacheService.Get<dynamic>(cacheSession.SessionId);
|
||||
}
|
||||
|
||||
public void SignIn(string loginName, dynamic userInfo, TimeSpan expiration)
|
||||
{
|
||||
var sessionId = cacheSession.SessionId;
|
||||
cacheService.Set(sessionId, userInfo, expiration);
|
||||
}
|
||||
|
||||
public void SignOut()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(CacheKey.UserID))
|
||||
{
|
||||
cacheService.Remove(CacheKey.UserID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Infrastructure/Auth/IAuthenticationService.cs
Normal file
31
Infrastructure/Auth/IAuthenticationService.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Infrastructure.Auth
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于身份认证接口
|
||||
/// </summary>
|
||||
public interface IAuthenticationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 登录
|
||||
/// </summary>
|
||||
/// <param name="loginName">登录名</param>
|
||||
/// <param name="userData">与登录名相关的用户信息</param>
|
||||
/// <param name="expiration">登录Cookie的过期时间,单位:分钟。</param>
|
||||
void SignIn(string loginName, dynamic userInfo, TimeSpan expiration);
|
||||
/// <summary>
|
||||
/// 注销
|
||||
/// </summary>
|
||||
void SignOut();
|
||||
/// <summary>
|
||||
/// 获取当前登录的用户
|
||||
/// </summary>
|
||||
/// <returns>当前用户未通过认证则返回null</returns>
|
||||
dynamic GetAuthenticatedUser();
|
||||
}
|
||||
}
|
||||
20
Infrastructure/Auth/UserContext.cs
Normal file
20
Infrastructure/Auth/UserContext.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Infrastructure.Auth
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前登录用户相关
|
||||
/// </summary>
|
||||
public class UserContext
|
||||
{
|
||||
public static dynamic CurrentUser
|
||||
{
|
||||
get
|
||||
{
|
||||
IAuthenticationService authenticationService = DIContainer.ResolvePerHttpRequest<IAuthenticationService>();
|
||||
var currentUser = authenticationService.GetAuthenticatedUser();
|
||||
if (currentUser != null)
|
||||
return currentUser;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user