CORS 구성 - ASP 네트 코어

10831 단어 corsdotnetcsharpasp

CORS 구성




API와 API 간의 상호 작용에 대한 정의, API 사용에 대한 요구 사항은 WS와 상호 작용할 수 있습니다. CORS 구성에 대한 정의입니다.

백엔드



Esta configuración se realiza en la clase **Startup** .

Declaras una variable solo de lectura, al inicio de la clase:

public class Startup
{
    readonly string MiCors = "MiCors";
        ...
        ...
}


Lo siguiente, es agregar la configuración del servicio en el metodo **ConfigureServices** :

services.AddCors(options =>
{
      options.AddPolicy(name: MiCors,
                        builder =>
                        {
                                builder.WithMethods("*");
                                builder.WithHeaders("*");
                            builder.WithOrigins("*");
                        });
});

**builder.WithOrigins("*"); -** Es donde se le indica al CORS qué dirección tendrá interacción con el WS. Si se deja el asterisco como tal, permitirá la conexión de cualquier dirección.

Por último es necesario agregar el **app.Use** , 최종 결정 사항:

app.UseRouting();

app.UseCors(MiCors);

app.UseAuthorization();


시작

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WSVenta.models;

namespace WSVenta
{
    public class Startup
    {
        readonly string MiCors = "MiCors";
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy(name: MiCors,
                                  builder =>
                                  {
                                      builder.WithOrigins("*");
                                  });
            });
            services.AddDbContext<VentaRealContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "WSVenta", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WSVenta v1"));
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors(MiCors);

            app.UseAuthorization();

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

좋은 웹페이지 즐겨찾기