IdentityServer4.CorsPolicyProvider:Warning: CorsPolicyService did not allow origin

Created on 15 Feb 2017  路  5Comments  路  Source: IdentityServer/IdentityServer4

I am getting this error when trying to post from a javascript html to http://localhost:3400/connect/token

IdentityServer4.CorsPolicyProvider:Warning: CorsPolicyService did not allow origin: http://localhost:8080

Chrome is returning:
XMLHttpRequest cannot load http://localhost:3400/connect/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.

My client:

    public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
        {
            new Client
            {
                ClientId = "XXXX",

                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

                // secret for authentication
                ClientSecrets =
                {
                    new Secret("XXXX".Sha256())
                },

                // scopes that client has access to                
                  AllowedScopes =
                {
                    "api1"
                },
                AccessTokenLifetime = 3600 ,
               AuthorizationCodeLifetime=600,
               AbsoluteRefreshTokenLifetime = 2592000,
               SlidingRefreshTokenLifetime = 1296000,
               RefreshTokenUsage = TokenUsage.OneTimeOnly,
               RefreshTokenExpiration = TokenExpiration.Sliding,
                    AllowedCorsOrigins = new List<string>
            {
                "http://127.0.0.1:8080",
                "http://localhost:8080",
                "*"

            }
            }
        };


        }

Startup.cs

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());


            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseApplicationInsightsRequestTelemetry();

            app.UseApplicationInsightsExceptionTelemetry();


            // app.UseMiddleware<KestrelAuthenticationMiddleware>();
            app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
            {
                Authority = "http://localhost:3400",
                RequireHttpsMetadata = false,

                ApiName = "api1"
            });
            app.UseIdentityServer();



            app.UseMvc();

        }

question

All 5 comments

Did you add services.AddCors(); to ConfigureServices ?

Check the IS logs for any relevant info.

Yes, here is my ConfigureServices

   public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            var mongodbClient = new MongoClient(Configuration.GetSection("Database").GetSection("URL").Value);
            var database = mongodbClient.GetDatabase(Configuration.GetSection("Database").GetSection("Name").Value);
            services.AddSingleton<IMongoDatabase>(database);
            services.AddSingleton(typeof(IRepository<>), typeof(MongodbRepository<>));
            services.AddSingleton(typeof(IService<>), typeof(GenericService<>));
            services.AddSingleton(typeof(IQuestionService), typeof(QuestionService));
            services.AddSingleton(typeof(IUserService), typeof(UserService));
            services.AddSingleton(typeof(IPaymentService), typeof(PaymentService));
            services.AddSingleton(typeof(IMongodbRepository<>), typeof(MongodbRepository<>));
            services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper());


            services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddInMemoryApiResources(IdentityServer.GetApiResources())
                .AddInMemoryClients(IdentityServer.GetClients());

            services.AddTransient<IResourceOwnerPasswordValidator, MongoDbResourceOwnerPasswordValidator>();
            services.AddTransient<IProfileService, MongoDbProfileService>();

            services.AddCors();

            services.AddMvc();



        }

Omg, my problem was I have my project "On build error"=> "Launch Old Version". I had a compile error but I ignored it because I thought it was just typescript error. So whatever changed I made never took effect.

I wasted 2 days on this.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

not-good-with-usernames picture not-good-with-usernames  路  3Comments

osmankibar picture osmankibar  路  3Comments

klioqc picture klioqc  路  3Comments

krgm03 picture krgm03  路  3Comments

wangkanai picture wangkanai  路  3Comments