Aar*_*son 0 c# authentication .net-5
我已向 Microsoft 的应用程序添加了身份验证。这是有效的,我可以看到用户,我被重定向到登录,一切都很好。
但是,如果我尝试访问中间件中的用户详细信息,它们总是为空?在控制器中我可以很好地读取一切。
public class CustomMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<CustomMiddleware> _logger;
public CustomMiddleware(RequestDelegate next, ILogger<CustomMiddleware> logger)
{
_next = next;
_logger = logger;
}
// IMyScopedService is injected into Invoke
public async Task Invoke(HttpContext httpContext)
{
_logger.LogInformation("WE ARE HERE {0}", httpContext.User.Identity.Name);
await _next(httpContext);
}
}
Run Code Online (Sandbox Code Playgroud)
[信息] 我们在这里&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
控制器
[HttpGet("private")]
public IActionResult Private() {
var user = new { User.Identity.Name, User.Identity.IsAuthenticated, User.Identity.AuthenticationType };
return Ok(user);
}
Run Code Online (Sandbox Code Playgroud)
{ 名称:“example@gmail.com”,isAuthenticated:true,authenticationType:“AuthenticationTypes.Federation”}
我尝试过两种中间件注册方法,但两者都在做同样的事情。
app.UseMiddleware<CustomMiddleware>();
services.AddTransient<IStartupFilter, ExampleStartupFilter>();
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
简而言之,这是您应用中间件的关键。在startup.cs中的Configure方法中,您必须按照以下方式放置方法。
如果你把中间放在后面
app.UseAuthentication();
app.UseAuthorization();
app.UseMiddleware<CustomMiddleware>(); // This is your middleware.
Run Code Online (Sandbox Code Playgroud)