简体   繁体   English

Cors在.Net Core 3.1项目中实现Oracle数据库主机

[英]Cors implementation in .Net Core 3.1 project with Oracle Database Host

Only one HOST can connect to the API, in this case it is an Oracle server.只有一个 HOST 可以连接到 API,在这种情况下,它是 Oracle 服务器。

I added Cors as per Microsoft docs, still other HOST can connect to my API.我根据 Microsoft 文档添加了 Cors,其他主机仍然可以连接到我的 API。 Has anyone tried to add an Oracle Database HOST?有没有人尝试添加 Oracle 数据库主机? Is there something wrong with the configuration?是不是配置有问题?

Startup.cs启动.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddHttpClient();

            services.AddCors(options =>
            {
                options.AddPolicy(name: "CorsPolicy",
                    builder =>
                    {
                        builder.WithOrigins("http://oraas1111.net:1001")
                                           .AllowAnyHeader()
                                           .AllowAnyMethod();
                    });
            });
        } 
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();

            app.UseCors("CorsPolicy");

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers().RequireCors("CorsPolicy");
            });
        }

Controller.cs Controller.cs

    [EnableCors("CorsPolicy")]
    [Route("[controller]")]
    [ApiController]
    public class Controller : ControllerBase
    {

        [HttpPost]
        [Route("/Service/[action]")]
        public async Task<ActionResult<Request>> Update(Request data)
        {

        }

        [HttpGet]
        [Route("/GET_Service/[action]")]
        public ActionResult Test()
        {
            return Ok();
        }
    }

You have to move services.AddCors() to the top, before AddControllers()您必须在 AddControllers() 之前将 services.AddCors() 移到顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM