I have web application buit in .net 8 and angular. Both web api and angular are part of same solution. Everything working fine with visual studio but when I deployed application on IIS I get error 405.
I tried following things.
- Enabling request filtering
- Enabling tracing
- Removing the handlers webDev etc
- Removed Filters and middleware’s
- Enabling the CORS for all the origins
Everything works fine on the local development but when I am deploying into the IIS server its returning 405 method not allowed error.
But tried lot on the IIS like enable Request filtering etc, but I cant resolve it.
Below is my program.cs and I suspect JWT Authentication
var builder = WebApplication.CreateBuilder(args);
#region Configure JWT authentication
// Load configuration from appsettings.json
builder.Configuration.AddJsonFile("appsettings.json");
// Configure JWT authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration.GetSection("Jwt:Issuer").Value,
ValidAudience = builder.Configuration.GetSection("Jwt:Audience").Value,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration.GetSection("Jwt:SecretKey").Value))
};
// Optionally, configure other JwtBearer options
});
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "l", Version = "v1" });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter the Bearer token",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
#endregion
builder.Services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/json" });
});
#region Add services to the container
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables();
var connection = builder.Configuration.GetConnectionString("PublicPortalDBcontext");
var SecurityKey = builder.Configuration.GetSection("Jwt:SecretKey");
var Issuer = builder.Configuration.GetSection("Jwt:Issuer");
var audience = builder.Configuration.GetSection("Jwt:Audience");
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddControllersWithViews();
//Services Initialization
builder.Services.AddDbContext<DbContext>(options =>
options.UseSqlServer(connection));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
#endregion
#region CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("EnableCORS", cbuilder =>
{
cbuilder.WithOrigins(builder.Configuration.GetSection("corsURL:devUrl").Value
, builder.Configuration.GetSection("corsURL:testUrl").Value
, builder.Configuration.GetSection("corsURL:prodUrl").Value)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
#endregion
#region LOGGER
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.CreateLogger();
builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);
#endregion LOGGER
#region Configure the HTTP request pipeline
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Portal V1");
c.RoutePrefix = "swagger"; // Optional: Set the Swagger UI route prefix
});
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
//exception handling and logging middleware
app.UseMiddleware<ExceptionHandlingMiddleware>();
app.UseCors("EnableCORS");
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseResponseCompression();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapFallbackToFile("/index.html");
app.Run();
#endregion
Request Header
accept:
application/json, text/plain, /
accept-encoding:
gzip, deflate, br, zstd
accept-language:
en-US,en;q=0.9,ta;q=0.8
connection:
keep-alive
content-length:
75
content-type:
application/json; charset=UTF-8
host:
localhost:85
origin:
http://localhost:85
referer:
http://localhost:85/
sec-ch-ua:
"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"
sec-ch-ua-mobile:
?0
sec-ch-ua-platform:
"Windows"
sec-fetch-dest:
empty
sec-fetch-mode:
cors
sec-fetch-site:
same-origin
user-agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
withcredentials:
true
Response Header
HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache,no-store
Pragma: no-cache
Transfer-Encoding: chunked
Allow: GET, HEAD
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: *
X-Powered-By: ASP.NET
Date: Mon, 12 Aug 2024 08:11:50 GMT
Please assist me to resolve this issues. Thanks in Advance.
user3184339 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.