Hi,
I trying enable cors in asp.net core 3.0
I added following code to ConfigureServices in Startup.cs
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("https://ogrencievi.net")
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
and Configure;
app.UseCors();
Startup.cs;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("https://ogrencievi.net").AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
services.AddDbContext<OEDBContext>(_ => _.UseSqlServer("Data Source=.... Initial Database=ogrencievidb.mdf;Integrated Security=false; User ID=..;Password=...; MultipleActiveResultSets=True;"));
services.Configure<AuthOptions>(Configuration.GetSection("AuthOptions"));
var authOptions = Configuration.GetSection("AuthOptions").Get<AuthOptions>();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
RequireExpirationTime = true,
ValidIssuer = authOptions.Issuer,
ValidAudience = authOptions.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authOptions.SecureKey)),
ClockSkew = TimeSpan.Zero,
};
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
The project working on api.ogrencievi.net and this code should add the Access-Control-Allow-Origin feature to HEADERS section but it doesn't work.
How can I fix it ?
What I have tried:
I did this by looking at this link.
https://docs.microsoft.com/tr-tr/aspnet/core/security/cors?view=aspnetcore-3.0 | Microsoft Docs[
^]>
I also examined this page and tried to implement it;
c# - How to enable CORS in ASP.net Core WebAPI - Stack Overflow[
^]