Files
simapi-net/Middlewares/SimApiAuthMiddleware.cs
T

42 lines
1.1 KiB
C#
Raw Normal View History

2020-02-04 16:51:15 +08:00
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
2020-08-05 15:29:56 +08:00
using SimApi.Communications;
2020-02-04 16:51:15 +08:00
2020-08-05 15:29:56 +08:00
namespace SimApi.Middlewares
2020-02-04 16:51:15 +08:00
{
/// <summary>
/// 认证信息获取中间件
/// </summary>
2020-08-05 15:29:56 +08:00
public class SimApiAuthMiddleware
2020-02-04 16:51:15 +08:00
{
private RequestDelegate Next { get; }
2020-08-05 15:29:56 +08:00
public SimApiAuthMiddleware(RequestDelegate next)
2020-02-04 16:51:15 +08:00
{
Next = next;
}
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
{
string token = null;
if (httpContext.Request.Headers.ContainsKey("Token"))
{
token = httpContext.Request.Headers["Token"];
}
if (!string.IsNullOrEmpty(token))
{
var login = cache.GetString(token);
if (login != null)
{
2020-08-05 15:29:56 +08:00
httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize<SimApiLoginItem>(login));
2020-02-04 16:51:15 +08:00
}
}
2020-08-29 11:49:44 +08:00
2020-02-04 16:51:15 +08:00
return Next(httpContext);
}
}
}