简体   繁体   中英

How to configure Windows authentication with ASP.NET Core 6.0 and Angular project

I am trying to configure Windows authentication with ASP.NET Core 6 Angular template. Here is the configuration I am using.

I have added following configuration to the program.cs file:

// Add services to the container.

builder.Services.AddControllersWithViews();
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;
});

I also added some middleware like this

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

I have also updated launchSettings.json like this:

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

And I have updated proxy.conf.js like this:

const PROXY_CONFIG = [
  {
    context: [
      "/weatherforecast",
   ],
    target: target,
    secure: false,
    changeOrigin: true,
    agent: new Agent({ //Also tried it without this agent
      maxSockets: 100,
      keepAlive: true,
      maxFreeSockets: 10,
      keepAliveMsecs: 100000,
      timeout: 6000000,
      keepAliveTimeout: 90000
    }),
    headers: {
      Connection: 'Keep-Alive',      
    },onProxyRes: proxyRes => {
      const key = "www-authenticate";
      proxyRes.headers[key] = proxyRes.headers[key] &&
        proxyRes.headers[key].split(",");
    }
  }
]

Here are changes related to http call

    http.get<WeatherForecast[]>(baseUrl + 'weatherforecast', { withCredentials: true }).subscribe(result => {
      this.forecasts = result;
    }, error => console.error(error));
  }

When I call the controller, I get an error code 400:

在此处输入图像描述

Seems like that your are using HTTPS. Agent is for HTTP only. For HTTPS, we must use HttpsAgent:

const HttpsAgent = require('agentkeepalive').HttpsAgent;

const PROXY_CONFIG = [
  {
    context: ["/api"],
    target: target,
    secure: false,
    changeOrigin: true,
    agent: new HttpsAgent({
      maxSockets: 100,
      keepAlive: true,
      maxFreeSockets: 10,
      keepAliveMsecs: 100000,
      timeout: 6000000,
      keepAliveTimeout: 90000
    }),
    onProxyRes: proxyRes => {
      const key = "www-authenticate";
      proxyRes.headers[key] = proxyRes.headers[key] &&
        proxyRes.headers[key].split(",");
    }
  },
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM