Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c68cfa6192 | ||
|
|
8344567ac6 | ||
|
|
99b26d99f6 |
@@ -40,9 +40,9 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
|
||||
{
|
||||
string token = null;
|
||||
|
||||
if (Request.Headers.ContainsKey("Token"))
|
||||
if (Request.Headers.TryGetValue("Token", out var value))
|
||||
{
|
||||
token = Request.Headers["Token"];
|
||||
token = value;
|
||||
}
|
||||
|
||||
auth.Logout(token);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
@@ -14,6 +15,8 @@ public class SimApiAuth(IDistributedCache cache)
|
||||
/// 产生一个Token记录并返回Token
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public string Login(string id, string type = "user", string token = null)
|
||||
{
|
||||
@@ -25,6 +28,7 @@ public class SimApiAuth(IDistributedCache cache)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="uuid"></param>
|
||||
/// <returns></returns>
|
||||
public string Login(string id, string[] type, string uuid = null)
|
||||
{
|
||||
@@ -34,6 +38,17 @@ public class SimApiAuth(IDistributedCache cache)
|
||||
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>
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using SimApi.Communications;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi.Middlewares;
|
||||
|
||||
@@ -11,7 +12,7 @@ namespace SimApi.Middlewares;
|
||||
/// </summary>
|
||||
public class SimApiAuthMiddleware(RequestDelegate next)
|
||||
{
|
||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache, SimApiAuth auth)
|
||||
{
|
||||
string token = null;
|
||||
if (httpContext.Request.Headers.TryGetValue("Token", out var header))
|
||||
@@ -21,10 +22,10 @@ public class SimApiAuthMiddleware(RequestDelegate next)
|
||||
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
var login = cache.GetString(token);
|
||||
var login = auth.GetLogin(token);
|
||||
if (login != null)
|
||||
{
|
||||
httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize<SimApiLoginItem>(login));
|
||||
httpContext.Items.Add("LoginInfo", login);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ public static class SimApiExtensions
|
||||
// 是否使用 AUTH
|
||||
if (simApiOptions.EnableSimApiAuth)
|
||||
{
|
||||
builder.AddScoped<SimApiAuth>();
|
||||
builder.AddSingleton<SimApiAuth>();
|
||||
}
|
||||
|
||||
if (simApiOptions.EnableCors)
|
||||
|
||||
Reference in New Issue
Block a user