using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Util.Tests.Middlewares;
///
/// 测试用户中间件
///
public class TestUserMiddleware {
///
/// 下个中间件
///
private readonly RequestDelegate _next;
///
/// 初始化测试用户中间件
///
/// 下个中间件
public TestUserMiddleware( RequestDelegate next ) {
_next = next;
}
///
/// 执行中间件
///
/// Http上下文
public async Task Invoke( HttpContext context ) {
var userId = context.Request.Headers["user-id"].SafeString();
if ( userId.IsEmpty() == false ) {
var identity = new ClaimsIdentity( new[] { new Claim( "sub", userId ) } );
context.User = new ClaimsPrincipal( identity );
}
await _next( context );
}
}