Visual Studio 2022 does not provide an option to scaffold an Angular application with Windows authentication. In this blog post I document the required steps to get Windows authentication working for Angular and .NET 7.
You can start with the default template in Visual Studio. Select "None" as "Authentication type"
In ClientApp/proxy.conf.js
you'll have to use the following setup (you may have to adjust the port 55781 in line 5)
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:55781';
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(",");
}
}
]
module.exports = PROXY_CONFIG;
In Program.cs
you'll have to add authentication und authorization:
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;
});
app.UseAuthentication();
app.UseAuthorization();
Now you can start to use [Authorize]
attributes in your controllers/actions.
Source Code
The complete source code can be found on GitHub.