.Net 6对SPA进行了反应,并且windows身份验证无法获得用户名



我有一个默认的react SPA页面,使用visual studio 2022和im使用windows身份验证。我的应用程序将仅在IIS托管的intranet应用程序上运行。

在我的Program.cs中,我配置了身份验证和授权:

builder.Services.AddControllersWithViews();
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddHttpContextAccessor();


builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = NegotiateDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = NegotiateDefaults.AuthenticationScheme;
}).AddNegotiate();

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

为了测试,我创建了一个方法应用程序。用于捕获用户名,当我使用IISExpress配置文件运行它时,我可以在主页上成功获取登录的windows用户if子句上的断点在应用程序上的每个请求中都被命中

app.Use(async (context, next) =>
{
    if (!context.User.Identity.IsAuthenticated)
    {
        await context.ChallengeAsync();
        await next();
    }
    else
    {
        
        await next();
    }
});

但是当我调用示例的端点";天气预报控制器";或者我没有在httpContext上获取登录用户,或者页面向我发送错误401.2-未经授权。我还将fetch调用上的凭据设置为";包括";

    async populateWeatherData() {
      const response = await fetch('weatherforecast', { "credentials": "include" });
  const data = await response.json();
  this.setState({ forecasts: data, loading: false });
}

我还将launchsettings.json设置为只接受windows身份验证

"iisSettings": {
  "windowsAuthentication": true,
  "anonymousAuthentication": false,
  "iisExpress": {
    "applicationUrl": "http://localhost:53168",
    "sslPort": 44381
  }

还尝试将InProcess更改为OutOfProcess构建,但出现相同错误

此外,我创建了一个迷你web.config,以设置安全标记以允许windows身份验证,并尝试修改.vs文件夹;应用程序>\config\applicationhost.config更改de windows和匿名身份验证配置

如果我在同一台机器上使用MVC项目,它就像一个魅力,我认为SPAProxy上有一个代理或其他东西会把搞砸

为了成功获得这些证书,我缺少了什么,有什么技巧或解决办法吗?

经过多次谷歌搜索和测试,我找到了一个解决方法:

  1. http代理中间件混淆了请求头,并且.Net Core不允许授权,不管我做什么;

  2. 在Program.cs中配置身份验证并启用cors,允许在所有应用程序中使用,或者仅在需要进行身份验证的控制器中使用。withorigins中的url是react开发服务器。对于实际应用程序,请使用appSettings<开发/暂存/生产>。json方法,并将该变量包含为地址字符串数组。为了在本地IIS和生产环境中进行调试,请安装IIS Cors模块,并通过Microsoft 下载

Program.cs

builder.Services.AddCors(options =>
{
options.AddPolicy("Padrao", builder =>
{
builder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.WithOrigins("https://localhost:44431");
});
});
builder.Services.AddAuthentication(configureOptions =>
{
configureOptions.DefaultAuthenticateScheme = NegotiateDefaults.AuthenticationScheme;
configureOptions.DefaultChallengeScheme = NegotiateDefaults.AuthenticationScheme;
}).AddNegotiate();
builder.Services.AddAuthorization();
var app = builder.Build();
  1. 使用React中的环境变量,注入所需的后端主机作为地址。重要提示:该变量的前端需要有whe REACT_APP_前缀。从jasonwatmore.com了解

.env.development

PORT=44431
HTTPS=true
REACT_APP_ASPNETCORE_URLS="https://localhost:44375/"

.env

REACT_APP_ASPNETCORE_URLS="https://localhost/testreact/"
  1. 使用带凭据的提取并保持活动标头

    async populateWeatherData() {
    const response = await fetch(process.env.REACT_APP_ASPNETCORE_URLS +'weatherforecast', {
    method: "GET",
    mode:'cors',
    credentials: 'include',
    headers: {
    "Connection": 'keep-alive',
    }
    });
    const data = await response.json();
    this.setState({ forecasts: data, loading: false });}
    
  2. 允许windows身份验证并禁用匿名身份验证。您需要以IISExpress配置文件的形式运行该应用程序,或者创建一个IIS配置文件进行调试,在launchsettings.json 中对其进行更改

    "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iis": {
    "applicationUrl": "http://localhost/testereact",
    "sslPort": 443
    },
    "iisExpress": {
    "applicationUrl": "http://localhost:43938",
    "sslPort": 44375
    }
    

若要允许Vs20220在本地IIS中运行,请在Visual Studio安装程序上安装de-IIIS集成。使用这种方法,您需要通过Powershell命令提示符在/Clientapp中调用npm-start(我没有自己调用)

我仍然没有在实际应用程序的生产环境中测试这种方法,但这解决了问题

最新更新