新增了一个获取登陆信息的方法
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
#nullable enable
|
||||||
using System;
|
using System;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
@@ -14,6 +15,8 @@ public class SimApiAuth(IDistributedCache cache)
|
|||||||
/// 产生一个Token记录并返回Token
|
/// 产生一个Token记录并返回Token
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Login(string id, string type = "user", string token = null)
|
public string Login(string id, string type = "user", string token = null)
|
||||||
{
|
{
|
||||||
@@ -25,6 +28,7 @@ public class SimApiAuth(IDistributedCache cache)
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <param name="type"></param>
|
/// <param name="type"></param>
|
||||||
|
/// <param name="uuid"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Login(string id, string[] type, string uuid = null)
|
public string Login(string id, string[] type, string uuid = null)
|
||||||
{
|
{
|
||||||
@@ -34,6 +38,17 @@ public class SimApiAuth(IDistributedCache cache)
|
|||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取登陆信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public SimApiLoginItem? GetLogin(string token)
|
||||||
|
{
|
||||||
|
var login = cache.GetString(token);
|
||||||
|
return login != null ? JsonSerializer.Deserialize<SimApiLoginItem>(login) : default;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 退出登陆
|
/// 退出登陆
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Middlewares;
|
namespace SimApi.Middlewares;
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ namespace SimApi.Middlewares;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SimApiAuthMiddleware(RequestDelegate next)
|
public class SimApiAuthMiddleware(RequestDelegate next)
|
||||||
{
|
{
|
||||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
public Task Invoke(HttpContext httpContext, IDistributedCache cache, SimApiAuth auth)
|
||||||
{
|
{
|
||||||
string token = null;
|
string token = null;
|
||||||
if (httpContext.Request.Headers.TryGetValue("Token", out var header))
|
if (httpContext.Request.Headers.TryGetValue("Token", out var header))
|
||||||
@@ -21,10 +22,10 @@ public class SimApiAuthMiddleware(RequestDelegate next)
|
|||||||
|
|
||||||
if (!string.IsNullOrEmpty(token))
|
if (!string.IsNullOrEmpty(token))
|
||||||
{
|
{
|
||||||
var login = cache.GetString(token);
|
var login = auth.GetLogin(token);
|
||||||
if (login != null)
|
if (login != null)
|
||||||
{
|
{
|
||||||
httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize<SimApiLoginItem>(login));
|
httpContext.Items.Add("LoginInfo", login);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user