我的CORS与.Net 6和Angular应用程序在本地工作,但在IIS中不工作

k2arahey  于 6个月前  发布在  .NET
关注(0)|答案(2)|浏览(85)

我花了2天的时间试图让CORS在前面使用我的Angular,在后面使用.Net 6 Core,我哭了。
我创建了一个简单的angular应用程序。只是一个“get”,它击中了我的后端API。在我的后端API中,我创建了一个简单的项目,我在我的program.cs中这样做了:

using Microsoft.AspNetCore.Authentication.Negotiate;

var builder = WebApplication.CreateBuilder(args);

var policyName = "_myAllowSpecificOrigins";

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: policyName,
        builder =>
        {
            builder
            .WithMethods("POST")
            .WithOrigins("http://localhost:4200");
            //.AllowAnyOrigin()
        });
});

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

var app = builder.Build();

app.UseCors(policyName);

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();

字符串
在我的简单控制器中:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    [EnableCors("_myAllowSpecificOrigins")]
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}


当我在localhost中测试时,我可以访问get结果:
x1c 0d1x的数据
即使我没有把GET放在withMethods中。
如果我把.WithOrigins("http://hhhhhh:4200");
我得到

当我把Angular和.Net Core应用程序放在IIS中时,它总是工作,而我把.WithOrigins("http://localhost:4200");我尝试了很多东西,但网站总是工作,我从来没有得到消息'No ' call-Control-Allow-Origin' header is present on the requested resource.'我如何设置IIS?
请帮帮忙!!谢谢。

ecfdbz9o

ecfdbz9o1#

通过查看您的政策,

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: policyName,
        builder =>
        {
            builder
            .WithMethods("POST")
            .WithOrigins("http://localhost:4200");
            //.AllowAnyOrigin()
        });
});

字符串
似乎你只允许POST请求。并且你尝试调用GET请求。为此,你需要在策略中做一些更改,如下所示:

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: policyName,
        builder =>
        {
            builder
            .WithMethods("POST", "GET")  // Allow both POST and GET
            .WithOrigins("http://localhost:4200");
            //.AllowAnyOrigin()
        });
});

kqlmhetl

kqlmhetl2#

CORS策略已阻止从源“http://localhost:4200”访问位于“”的XMLHttpRequest:请求的资源上不存在“”控制-允许-源“”标头。
如果您尝试从Angular应用程序访问跨域后端API,您可能会从源“http://localhost:4200”被CORS策略错误阻止。此错误是由浏览器中的同源策略和跨源资源共享(CORS)机制引起的。
在错误消息中,您试图从http://localhost:4200的源访问资源http://localhost:5112/WeatherForecast。但服务器未设置相应的CORS标头以允许此跨域请求,因此浏览器阻止了该请求。
要解决此问题,您需要在响应中包含EST-Control-Allow-Origin标头,该标头指定允许访问资源的域:

Access-Control-Allow-Origin: http://localhost:4200

字符串
Microsoft IIS CORS模块是一个扩展,使网站能够支持CORS(跨域资源共享)协议。如果您想了解如何在IIS中使用CORS模块,请参阅:IIS CORS module Configuration Reference

相关问题