Muhammad Nasir
Muhammad Nasir

Reputation: 2204

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:

enter image description here

Upvotes: 4

Views: 1549

Answers (2)

Marcomoto
Marcomoto

Reputation: 148

Seems like that your are using HTTPS. Agent is for HTTP only. For HTTPS, you have to 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,
      freeSocketTimeout: 90000
    }),
    onProxyRes: proxyRes => {
      const key = "www-authenticate";
      proxyRes.headers[key] = proxyRes.headers[key] &&
        proxyRes.headers[key].split(",");
    }
  },
];

Upvotes: 2

Yurii Smohol
Yurii Smohol

Reputation: 19

const { env } = require('process');
const HttpsAgent = require('agentkeepalive').HttpsAgent;

const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
  env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:19537';

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

module.exports = PROXY_CONFIG;

Upvotes: 1

Related Questions