You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB
C#

8 months ago
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using NPin.Framework.Upms.Domain.Shared.Consts;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Claims;
namespace NPin.Framework.Upms.Domain.Authorization;
/// <summary>
/// RefreshToken 处理中间件
/// </summary>
public class RefreshTokenMiddleware : IMiddleware, ITransientDependency
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var refreshToken = context.Request.Headers["refresh_token"].ToString();
if (!refreshToken.IsNullOrEmpty())
{
var authResult = await context.AuthenticateAsync(TokenTypeConst.Refresh);
// Token刷新成功
if (authResult.Succeeded)
{
var userId = Guid.Parse(authResult.Principal.FindFirst(AbpClaimTypes.UserId).Value);
// TODO
// var accessToken =
// var refreshToken =
context.Response.Headers["access_token"] = "";
context.Response.Headers["refresh_token"] = "";
// 请求头替换
context.Request.Headers["Authorization"] = $"Bearer {""}";
}
}
await next(context);
}
}
/// <summary>
/// 扩展
/// </summary>
public static class RefreshTokenExtensions
{
public static IApplicationBuilder UseRefreshToken(this IApplicationBuilder builder)
{
builder.UseMiddleware<RefreshTokenMiddleware>();
return builder;
}
}