【发布时间】:2017-07-26 19:47:36
【问题描述】:
有没有办法通过中间件在 ASP NET Core 应用程序中设置用户名?
我创建了一个AnonymousUserMiddleware:
public AnonymousUserMiddleware(RequestDelegate next, HttpContextCurrentClaimsPrincipalAccessor currentClaimsPrincipalAccessor, ILogger<AnonymousUserMiddleware> logger)
{
_next = next;
_currentClaimsPrincipalAccessor = currentClaimsPrincipalAccessor;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
try
{
_currentClaimsPrincipalAccessor?.Current?.Identity?.Name = "anonymous";
await _next.Invoke(context);
}
catch (Exception ex)
{
_logger.LogError(1, ex, "Exception");
throw ex;
}
}
但这不起作用,因为Name 是一个只读字段。
我想在开发者模式下在Startup.cs 中调用它,以便在请求期间用户名始终是“匿名的”。
有没有办法做到这一点?
【问题讨论】:
标签: c# asp.net-core asp.net-identity