2020-09-27

Razor Pages app with Web API - how to disable CORS [closed]

I think, I have a problem with my Razor Pages app. The app is hosted on Google Cloud Run, not localhost. In the project, I have small Web API consist of two controllers, but that is not important. The problem is that when I try to request the API from my browser (that is not in the same domain) I can easily get all the data that API should provide in form of JSON objects. As I know (and the ASP .NET Core docs confirms this) CORS, in default, should be disabled. I don't have any code in Startup class and Controller class that enables CORS. Any ideas what could be wrong? Or I just don't understand the CORS? :)

Configure method in Startup class:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoleManager<IdentityRole> roleManager, UserManager<IdentityUser> userManager)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for Cableion scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            var cultureInfo = new CultureInfo("en-US");
            CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
            CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

            var proxyOptions = new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
            };
            proxyOptions.KnownProxies.Add(IPAddress.Parse("::ffff:111.111.1.111"));
            app.UseForwardedHeaders(proxyOptions);

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseCookiePolicy();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
            });

            CreateRolesAsync(roleManager).Wait();
            CreateSuperUser(userManager).Wait();

            var rootPath = env.WebRootPath;
            RotativaConfiguration.Setup(rootPath, "Rotativa");
        }

And Controller class:

    [Route("api/[controller]")]
    [ApiController]
    public class ToolController : ControllerBase
    {
        private readonly IToolRepository ToolData;

        public ToolController(IToolRepository ToolData)
        {
            this.ToolData = ToolData;
        }

        // GET: api/Tool
        [HttpGet]
        public IEnumerable<Tool> GetTools()
        {
            return ToolData.GetAll();
        }

        // GET: api/Tool/5
        [HttpGet("{id}", Name = "Get")]
        public Tool Get(int id)
        {
            return ToolData.GetById(id);
        }
    }


from Recent Questions - Stack Overflow https://ift.tt/2S5SViI
https://ift.tt/eA8V8J

No comments:

Post a Comment