CORS安装程序在AspNetCore2.2上不起作用

时间:2019-07-11 06:36:58

标签: asp.net-core .net-core cors angular7 asp.net-core-webapi

我有客户端应用程序(Angular7)和Web API(AspNetCore 2.2)。

我正在从客户端调用Facebook登录,它在我的浏览器中返回https://www.facebook.com/v3.1/dialog/oauth。但是,浏览器无法打开链接。这是我看到的错误。 console error

我还可以在网络标签上看到Facebook链接,但是它具有Request Method: OPTIONS request option in network tab。这有什么问题吗?

这是我的CORS设置代码。

        services.AddCors(options =>
        {
            options.AddPolicy("AnyOrigins",
            builder =>
            {
                builder.WithOrigins("http://localhost:4200")
                       .AllowAnyHeader()
                       .AllowAnyMethod()
                       .AllowCredentials();
            });
        });
        //services.AddSwaggerGen(c =>
        //{
        //    c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Developer API", Version = "v1" });
        //});

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;                
            options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
        })
            .AddCookie(options =>
            {
                options.ExpireTimeSpan = TimeSpan.FromDays(30);
                options.SlidingExpiration = true;
                options.Cookie.HttpOnly = true;
                options.Cookie.SameSite = SameSiteMode.None;                    
                options.LoginPath = "/login";
            })
            .AddJwtBearer(op =>
            {
                op.Audience = "auctioner-token";
                op.Authority = "http://localhost:5000";
                op.RequireHttpsMetadata = false;
                op.SaveToken = true;
            })
            .AddFacebook("facebook", facebookOptions =>
            {
                facebookOptions.AppId = "**********";
                facebookOptions.AppSecret = "************";
                facebookOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                facebookOptions.SaveTokens = true;
                facebookOptions.Scope.Add("public_profile");
                facebookOptions.Scope.Add("email");

                facebookOptions.Events.OnCreatingTicket = ctx =>
                {
                    List<AuthenticationToken> tokens = ctx.Properties.GetTokens().ToList();
                    tokens.Add(new AuthenticationToken()
                    {
                        Name = "TicketCreated",
                        Value = DateTime.UtcNow.ToString(),
                    });
                    ctx.Properties.IsPersistent = true;
                    ctx.Properties.StoreTokens(tokens);

                    return Task.CompletedTask;
                };
            });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddTransient<IRepository, Repository>();   

这里是配置代码:

        if(env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseCors("AnyOrigins");
        app.UseStaticFiles();

        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(), "swagger")),
            RequestPath = "/swagger"
        });
        //app.UseSwagger();
        //app.UseSwaggerUI(c =>
        //{
        //    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Developer API");
        //});

        app.UseCookiePolicy();
        app.UseAuthentication();


        app.UseMvc(routes =>
        {
            routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
        });

我坚持了几个星期。请帮忙。预先谢谢你。

0 个答案:

没有答案