Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

C#
public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        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();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        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[^]
Posted
Comments
Maciej Los 17-Apr-20 8:40am    
Try to add in Configure method this line:
app.UseCors(        options => options.WithOrigins("https://ogrencievi.net").AllowAnyMethod()


And remove similar line from ConfigureServices.
Kaan Öztürk 18-Apr-20 5:49am    
I've tried but didn't work :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900