Search code examples
.netangularasp.net-corewindows-authentication

dotnet run - angular - windows authentication - not authenticated


When running angular with:

ng serve --proxy-config proxy.config.js

and .net core with:

dotnet run

windows authentication never authenticate user.

However when running the application from Visual Studio F5 user is authenticated, (same port and everything).

proxy.config.js

const Agent = require('agentkeepalive');

module.exports = {
        '/api': {
            target: 'http://localhost:5000',
            secure: false,
            agent: new Agent({
                maxSockets: 100,
                keepAlive: true,
                maxFreeSockets: 10,
                keepAliveMsecs: 100000,
                timeout: 6000000,
                keepAliveTimeout: 90000
            }),
            onProxyRes: proxyRes => {
                let key = 'www-authenticate';
                proxyRes.headers[key] = proxyRes.headers[key] &&
                    proxyRes.headers[key].split(',');
            }
        }
};

iisSettings

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

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(IISDefaults.AuthenticationScheme);

            services.AddMvc(config =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                config.Filters.Add(new AuthorizeFilter(policy));
            });

            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.Use(async (context, next) =>
            {
                await next();
                if (context.Response.StatusCode == 404 &&
                    !Path.HasExtension(context.Request.Path.Value) &&
                    !context.Request.Path.Value.StartsWith("/api/"))
                {
                    context.Request.Path = "/index.html";
                    await next();
                }
            });

            app.UseMvcWithDefaultRoute();

            app.UseDefaultFiles();

            app.UseStaticFiles();
        }
    }

Why is that dotnet run and dotnet watch run doesn't work with windows authentication but Visual Studio F5 does?

Update

I have tried adding "weblistener" instead of enabling authentication in project properties. After adding weblistener I was able to authenticate using dotnet run but I could no longer start debugging using VS F5 for some reason...

.UseHttpSys(options =>
            {
                options.Authentication.Schemes = AuthenticationSchemes.NTLM;
                options.Authentication.AllowAnonymous = false;
            })

Solution

  • Windows authentication is not supported in dotnet cli if you're not using HTTPSYS or Weblistener, (weblistener replaced with httpsys in 2.0?)

    No Windows, no Windows auth.

    I'm not sure how you're supposed to develop locally if you're publishing your app in IIS, (without httpsys or weblistener), (not supported in IIS).

    I'm currently using IIS Express which is a pain in the butt.

    Source