2

I have created a new ASP.NET Web Application -> Web API 2 project with Windows Authentication. This works fine in IIS Express but to get hot reloading for my React front end I have tried to use webpack-dev-server and have it proxy my IIS Express. I have used this before both with cookie authentication and token (Bearer) authentication without a problem but with Windows Authentication (NTLM Authentication) it does not work.

Looking at the response from the server I get the expected 401 Unauthorized (401.2) response with the header www-authenticate: Negotiate, NTLM as expected but I don't think the client re-sends the request with Authorization: NTLM.

Browsers that I have tried with (Chrome, Firefox and IE) does not give me the normal prompt to enter correct credentials either.

https://blogs.msdn.microsoft.com/chiranth/2013/09/20/ntlm-want-to-know-how-it-works/

My settings in webpack.config.js looks like this:

var proxy = 'localhost:57263';

devServer: {
    proxy: {
        '*': {
            target: 'http://' + proxy,
            changeOrigin: true,
        },
        port: 8080,
        host: '0.0.0.0',
        hot: true,
    },
}
Ogglas
  • 62,132
  • 37
  • 328
  • 418

1 Answers1

8

After looking at the documentation for webpack-dev-server Proxy I saw that they use http-proxy-middleware.

https://webpack.github.io/docs/webpack-dev-server.html#proxy

This led me to this thread and answer:

https://github.com/chimurai/http-proxy-middleware/issues/39#issuecomment-330911943

Final solution:

Run:

npm install agentkeepalive --save

Working code:

var proxy = 'localhost:57263';

devServer: {
    proxy: {
        '*': {
            target: 'http://' + proxy,
            changeOrigin: true,
            agent: new agent({
                maxSockets: 100,
                keepAlive: true,
                maxFreeSockets: 10,
                keepAliveMsecs: 100000,
                timeout: 6000000,
                keepAliveTimeout: 90000 // free socket keepalive for 90 seconds
            }),
            onProxyRes: (proxyRes) => {
                var key = 'www-authenticate';
                proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
            },
        },
        port: 8080,
        host: '0.0.0.0',
        hot: true,
    },
}
Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • Hi Ogglas, How do i get the IISExpress and the webpackdevserver to work together. here is the issue that i face https://stackoverflow.com/questions/54216912/how-to-configure-webpackdevserver-to-run-an-app-with-asp-net-core-2-0-iisexpress any help is greatly appreciated – user581157 Jan 16 '19 at 12:30
  • 1
    Thanx, works great. But for target with https you need to use HttpsAgent: const agent = require('agentkeepalive').HttpsAgent; – Hyperdingo Nov 12 '21 at 11:54