【问题标题】:cors error in authentication type windows - visual studio 2019身份验证类型窗口中的 cors 错误 - Visual Studio 2019
【发布时间】:2022-09-23 09:39:55
【问题描述】:

我开始了一个 .NET CORE 5 项目

我选择了windows类型认证

type authentication image

这种类型的项目在客户端给我一个CORS错误(反应)

但是,如果我没有选择 windows 而不是 windows,我将不会收到错误

这是来自客户端的调用:

const res = await fetch(`https://localhost:44373/weatherforecast`)

我需要这种类型的项目,因为我想使用 AD 身份验证

我尝试将其添加到 fetch 调用中:

const res = await fetch(`https://localhost:44300/weatherforecast`,{credentials: \'include\'})

并更改启动:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication3
{
    public class Startup
    {
        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.AddControllers();
            services.AddCors(options =>
            {
                options.AddPolicy(\"MyMyAllowCredentialsPolicy\",
                    policy =>
                    {
                        policy.WithOrigins(\"https://localhost:44300\")
                               .AllowCredentials();
                    });
            });
        }

        // 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.UseHttpsRedirection();

            app.UseCors();

            app.UseRouting();

             app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
  • 恐怕你可以在你的 fetch 方法中尝试include the credential --> fetch(\'https://xxx\', {credentials: \'include\'})enable credential 在你的 api 中 ---> policy.WithOrigins(\"http://example.com\").AllowCredentials();
  • 它对我不起作用,我将 STARTUP 文件附加到问题中
  • 您是否使用 .AllowAnyOrigin() 方法进行了测试?我想这会工作
  • 查了一下,改成小王说的了。。
  • 起初我认为 Cors 问题可能与 cors 策略有关,但正如您提到的禁用 Windows 身份验证将使其工作,所以我认为您已经设置了 cors 策略,所以我推断它可能与AllowCredentials() 有关。因为它仍然无法工作。我发现this question 看起来和你的场景很相似,你能看看吗?

标签: asp.net-core windows-authentication webapi


【解决方案1】:

我还创建了一个带有 Windows 身份验证的新 .net 6 web api 项目。我也有一个前端项目。

这是我的代码,它对我有用。在我的Program.cs 中,我添加了 Cors 策略,其他策略是默认生成的。

using Microsoft.AspNetCore.Authentication.Negotiate;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCors(options =>
{
    options.AddPolicy("mypolicy",
        policy =>
        {
            policy.WithOrigins("http://localhost:8848").AllowCredentials();
                   //.AllowCredentials();
        });
});
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


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

builder.Services.AddAuthorization(options =>
{
    // By default, all incoming requests will be authorized according to the default policy.
    options.FallbackPolicy = options.DefaultPolicy;
});

var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("mypolicy");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

而且我使用ajax发送请求,缺少withCredentials会导致401错误:

$("#btn2").click(function(event) {
                $.ajax({
                    url: "https://localhost:7272/WeatherForecast",
                    type: "get",
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function(data) {
                        alert(data);
                        console.info(data);
                    }
                })
            });

还有一点需要注意的是,在私密模式下打开客户端网站时,在正常窗口打开网站一切正常的情况下,还是会遇到401。那是因为私有模式不包含我认为的身份验证信息。

对于 Post 请求,仍然有效。

带参数发帖??

【讨论】:

  • 我更改为 AJAX 调用,但带有参数的 POST 调用对我不起作用
  • 仍然有效....说实话我不知道为什么参数会影响请求,我误解了吗?
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 2012-11-22
  • 2019-09-18
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多