aspdotnetcore 跨域问题

发布于 2022-09-11 23:39:31 字数 2946 浏览 19 评论 0

前端是 vue 后端是aspdotnet core 2.2webapi
前端调用后端接口的时候不通:

Access to XMLHttpRequest at 'http://127.0.0.1:13037/api/user/authenticate' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

应该是跨域的问题,不知道的怎样才能让这个'Access-Control-Allow-Origin'加出来

接口的Startup中应该是已经用了跨域了

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();

            // configure strongly typed settings objects
            var appSettingsSection = Configuration.GetSection("AppSettings");
            services.Configure<AppSettings>(appSettingsSection);

            // configure jwt authentication
            var appSettings = appSettingsSection.Get<AppSettings>();
            var key = Encoding.ASCII.GetBytes(appSettings.Secret);
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });
            // https://stackoverflow.com/a/54429647/6011995
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // configure DI for application services
            services.AddScoped<IUserService, UserService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseCors(options =>
            options.WithOrigins("http://localhost:8080")
            .AllowAnyMethod()
            .AllowAnyHeader());
            app.UseHttpsRedirection();
            app.UseAuthentication();
            app.UseMvc();
        }
export const login = (username, password) => {
    return axios.request({        
        url: baseURL + '/api/user/authenticate',
        'Content-Type': 'application/json; charset=utf-8',
        data: {
            'Username': username,
            'Password': password
        }
    })
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2022-09-18 23:39:31
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
    
   

 services.AddCors(options =>
        {
            options.AddPolicy(MyAllowSpecificOrigins,
            builder =>
            {
                builder.WithOrigins("http://127.0.0.1:8080",
                                    "http://localhost:8080");
            });
        });
    
    
app.UseCors(MyAllowSpecificOrigins);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文