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;
///
/// RefreshToken 处理中间件
///
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 {accessToken}";
}
}
await next(context);
}
}
///
/// 扩展
///
public static class RefreshTokenExtensions
{
public static IApplicationBuilder UseRefreshToken(this IApplicationBuilder builder)
{
builder.UseMiddleware();
return builder;
}
}