Updat for CORS Resolution

This commit is contained in:
Santhosh S 2023-11-10 14:09:43 -05:00
parent d0650c36f3
commit 43eb29c2e8
8 changed files with 81 additions and 11 deletions

View File

@ -8,10 +8,16 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
// Add Swagger/OpenAPI documentation support.
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
@ -28,7 +34,6 @@ builder.Services.AddDbContext<AnswerDbContext>(option =>
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
@ -36,8 +41,11 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
app.UseAuthorization();
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthorization();
// Map controllers to their respective routes.
app.MapControllers();
app.Run();

View File

@ -8,12 +8,18 @@ using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
// Add Swagger/OpenAPI documentation support.
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
@ -45,6 +51,8 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthorization();
app.UseHttpsRedirection();

View File

@ -6,6 +6,11 @@ using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
// Add services to the container.
builder.Services.AddControllers();
@ -36,6 +41,9 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthorization();
app.MapControllers();

View File

@ -6,12 +6,18 @@ using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
// Add Swagger/OpenAPI documentation support.
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
@ -43,6 +49,9 @@ if (app.Environment.IsDevelopment())
}
}
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthorization();
app.MapControllers();

View File

@ -5,13 +5,18 @@ using Microsoft.EntityFrameworkCore;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
// Add services to the container.
// Add controller services and API Explorer for endpoint discovery.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
// Add Swagger/OpenAPI documentation support.
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
@ -44,6 +49,9 @@ if (app.Environment.IsDevelopment())
}
}
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthorization();
app.MapControllers();

View File

@ -15,8 +15,14 @@ builder.Services.AddControllers();
builder.Services.AddScoped<IQuestionsProvider, QuestionsProvider>();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
// Add controller services and API Explorer for endpoint discovery.
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
// Add Swagger/OpenAPI documentation support.
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
@ -44,6 +50,8 @@ if (app.Environment.IsDevelopment())
}
}
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthorization();
app.MapControllers();

View File

@ -16,6 +16,10 @@ const int intervalForCircuitBraker = 5; //5 seconds
// Add services to the container.
builder.Services.AddControllers();
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddScoped<ISurveysResponse, SurveyResponsesProvider>();
@ -36,6 +40,7 @@ builder.Services.AddHttpClient<IHttpUtil, HttpUtil>().
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
// Add Swagger/OpenAPI documentation support.
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
@ -56,6 +61,8 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthorization();
app.MapControllers();

View File

@ -10,7 +10,11 @@ using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Retrieve the security key for JWT token validation.
var authkey = builder.Configuration.GetValue<string>("JwtSettings:securitykey");
// Configure JWT authentication and validation.
builder.Services.AddAuthentication(item =>
{
item.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
@ -29,12 +33,18 @@ builder.Services.AddAuthentication(item =>
};
});
// CORS setup to allow requests from any origin.
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
// Add controller services and API Explorer for endpoint discovery.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddScoped<ISurveyProvider, SurveysProvider>();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
// Add Swagger/OpenAPI documentation support.
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
@ -42,10 +52,13 @@ builder.Services.AddSwaggerGen(c =>
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
// Configure the database context for SurveysDbContext using SQL Server.
builder.Services.AddDbContext<SurveysDbContext>(option =>
{
option.UseSqlServer("SurveyConnection");
});
var app = builder.Build();
// Configure the HTTP request pipeline.
@ -53,7 +66,7 @@ if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
// Seed initial data in the database when in development mode
using (var serviceScope = app.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
@ -61,10 +74,11 @@ if (app.Environment.IsDevelopment())
surveyProvider.seedData();
}
}
// Enable CORS, authentication, and authorization middleware.
app.UseCors("DamageAppCorsPolicy");
app.UseAuthentication();
app.UseAuthorization();
// Map controllers to their respective routes.
app.MapControllers();
app.Run();