小编Chr*_*jon的帖子

在ASP.NET Core中允许Cors Origin

我正在使用Microsoft.ApsNetCore.Cors 2.2

“ CORS策略已阻止从来源“ example.local”访问“ exampleapi.local”处的XMLHttpRequest:在请求的资源上不存在“ Access-Control-Allow-Origin”标头。”

我用这个设定设定

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigin",
                        builder =>
                        {
                            builder                            
                            .AllowAnyOrigin()
                            .AllowAnyMethod()
                            .AllowAnyHeader();
                        });
            });

            services.Configure<TokenSettings>(this.Configuration.GetSection("Tokens"));
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(opt =>
                {
                    opt.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,
                        ValidIssuer = Configuration["Tokens:Issuer"],
                        ValidAudience = Configuration["Tokens:Audience"],
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["Tokens:SecurityKey"]))
                    };
                });

            services.AddMvc();
            services.Configure<LdapConfig>(Configuration.GetSection("ldap"));
            services.AddScoped<ILdapAuthenticationService, LdapAuthenticationService>();
            services.AddScoped<IUserService, UserService>();
            services.AddScoped<IProjectService, ProjectService>();
            services.AddScoped<IProjectMembersService, ProjectMembersService>();
            services.AddScoped<IJourneyUsersService, JourneyUsersService>();
            services.AddScoped<IProjectRolesService, ProjectRolesService>();
            services.AddScoped<IPmoGuardianService, PmoGuardianService>();
            services.AddScoped<IHolidaysService, HolidaysService>();
            services.AddScoped<IMailService, MailService>(); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api asp.net-core

5
推荐指数
1
解决办法
2495
查看次数

标签 统计

asp.net-core ×1

asp.net-web-api ×1

c# ×1