我想知道是否可以将值从中间件发送到 controllerAPI?
例如,我想捕获一个特定的 header 并发送到 Controller 。
类似的东西:
public class UserTokenValidatorsMiddleware
{
private readonly RequestDelegate _next;
//private IContactsRepository ContactsRepo { get; set; }
public UserTokenValidatorsMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (!context.Request.Path.Value.Contains("auth"))
{
if (!context.Request.Headers.Keys.Contains("user-token"))
{
context.Response.StatusCode = 400; //Bad Request
await context.Response.WriteAsync("User token is missing");
return;
}
// Here I want send the header to all controller asked.
}
await _next.Invoke(context);
}
}
#region ExtensionMethod
public static class UserTokenValidatorsExtension
{
public static IApplicationBuilder ApplyUserTokenValidation(this IApplicationBuilder app)
{
app.UseMiddleware<UserTokenValidatorsMiddleware>();
return app;
}
}
#endregion
最佳答案
我所做的就是利用这些东西:
- 依赖注入(inject)(Unity)
ActionFilterAttribute
(因为我可以访问 IDependencyResolver)HierarchicalLifetimeManager
(所以每个请求我都会得到一个新实例)(阅读依赖范围)
Action 过滤器
public class TokenFetcherAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var token = actionContext.Request.Headers.Authorization.Parameter;
var scheme = actionContext.Request.Headers.Authorization.Scheme;
if (token == null || scheme != "Bearer")
return;
var tokenProvider = (TokenProvider) actionContext.Request.GetDependencyScope().GetService(typeof(TokenProvider));
tokenProvider.SetToken(token);
}
}
token 提供者
public class TokenProvider
{
public string Token { get; private set; }
public void SetToken(string token)
{
if(Token != null)
throw new InvalidOperationException("Token is already set in this session.");
Token = token;
}
}
统一配置
container.RegisterType<TokenProvider>(new HierarchicalLifetimeManager()); // Gets a new TokenProvider per request
Controller
[TokenFetcher]
public class SomeController : ApiController
{
private TokenProvider tokenProvider;
// The token will not be set when ctor is called, but will be set before method is called.
private string Token => tokenProvider.Token;
public SomeController(TokenProvider provider)
{
tokeProvider = provider;
}
public string Get()
{
return $"Token is {Token}";
}
}
更新
对于 asp.net core 使用内置的 DI 容器。
将 TokenProvider
注册为 Transient,以便为每个请求获取一个新的:
services.AddTransient<TokenProvider>();
关于.net - 是否可以从 aspnet core api 中的中间件向 Controller 发送值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39017590/