C# 从托管在Nginx代理服务器上的API ASP.Net 5获取用户标识
我正在使用ASP.Net 5构建API并获取User.Identity.Name 当我在Kestrel上调试并从浏览器调用该方法时,它工作正常。 但无论是从postman还是在Nginx代理服务器上部署后调用它,它都会给我401未经授权的权限 这是我的Startup.csC# 从托管在Nginx代理服务器上的API ASP.Net 5获取用户标识,c#,nginx,C#,Nginx,我正在使用ASP.Net 5构建API并获取User.Identity.Name 当我在Kestrel上调试并从浏览器调用该方法时,它工作正常。 但无论是从postman还是在Nginx代理服务器上部署后调用它,它都会给我401未经授权的权限 这是我的Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configurati
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpContextAccessor();
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
services.AddSingleton<ValidateAuthentication>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMiddleware<ValidateAuthentication>();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
internal class ValidateAuthentication : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
if (context.User.Identity.IsAuthenticated)
await next(context);
else
await context.ChallengeAsync();
}
}
[ApiController]
[Authorize(AuthenticationSchemes = NegotiateDefaults.AuthenticationScheme)]
public class UserController : ControllerBase
{
private readonly IHttpContextAccessor _httpContextAccessor;
public UserController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
[HttpGet]
[Route("api/user")]
[AllowAnonymous]
public string GetUserName()
{
string userName = _httpContextAccessor.HttpContext.User.Identity.Name;
return userName;
}
[HttpGet]
[Route("api/IsAuth")]
[AllowAnonymous]
public bool IsAuth()
{
bool isAuth = _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated;
return isAuth;
}
}