Я создал промежуточное программное обеспечение для этого эта работа, вероятно, сработала во время разработки. однако, когда я публикую выпуск и развертываю его в IIS, я получаю следующую ошибку
Код: Выделить всё
The Negotiate Authentication handler cannot be used on a server that directly supports Windows Authentication. Enable Windows Authentication for the server and the Negotiate Authentication handler will defer to it.
Код: Выделить всё
// Dependency Injection.cs
string? authScheme = configuration["AuthenticationScheme"];
if (authScheme) {
JwtTokenConfig? jwtTokenConfig = configuration.GetSection("jwtTokenConfig").Get();
services.AddSingleton(jwtTokenConfig);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>
{
// options
}).AddNegotiate();
// JWT Authentication (Anonymous)
services.AddScoped();
services.AddHostedService(sp => new JwtRefreshTokenCache(new JwtAuthManager(jwtTokenConfig)));
// Add CORS services
services.AddCors(options =>
{options.AddPolicy("AllowSpecificOrigin",
builder => builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
}
}
Код: Выделить всё
public class AuthMiddleware
{
private readonly RequestDelegate _next;
private readonly IConfiguration _configuration;
public AuthMiddleware(RequestDelegate next, IConfiguration configuration)
{
_next = next;
_configuration = configuration;
}
public async Task InvokeAsync(HttpContext context)
{
var authScheme = _configuration["AuthenticationScheme"];
var publicRoutes = new List
{ "/",
"/api/v1",
"/api/v1/Account/Login" };
if (publicRoutes.Any(route => context.Request.Path.StartsWithSegments(route, StringComparison.OrdinalIgnoreCase)))
{ await _next(context); return; }
AuthenticateResult result = null;
if (authScheme == "Windows") { result = await context.AuthenticateAsync(NegotiateDefaults.AuthenticationScheme); }
else if (authScheme == "JwtBearer")
{ result = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme); }
if (result != null && result.Succeeded)
{ context.User = result.Principal; await _next(context); }
else { await context.ChallengeAsync(); }
}
}
Код: Выделить всё
// [Authorize(AuthenticationSchemes = IISDefaults.AuthenticationScheme)]
[Authorize]
public class BaseController : Controller
Код: Выделить всё
app.UseAuthentication();
app.UseMiddleware();
app.UseAuthorization();
Я пытался реализовать это без Negotiate.если я отключу проверку подлинности Windows в IIS, Интернет полностью остановится с ошибкой 404, если я включу проверку подлинности Windows, мне будет предложено войти в систему через нее, и даже если я это сделаю, ничего не произойдет.
приложение полностью выключено, ни одна страница не отображается, даже страница входа в систему, потому что ошибка в program.cs, поэтому вообще ничего не работает.
Подробнее здесь: https://stackoverflow.com/questions/786 ... -authentic