dotnet run - angular - windows authentication - not authenticated

大憨熊 提交于 2019-12-11 06:38:12

问题


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;
            })

回答1:


If shortly: dotnet runstarts the app without using IIS as a reverse proxy and so all your IIS settings are ignored


Your launchSettings.json where you have iisSettings section is used only when you run an app from Visual Studio.

This json file holds project specific settings associated with each debug profile, Visual Studio is configured to use to launch the application, including any environment variables that should be used.

When you execute dotnet run command, the Web Server (Kestrel by default) starts and hosts the app.

When you launch the app from VS, also the IIS Express instance is configured to be a reverse proxy for your application. And this is enabling Windows Authentication.

Look into IIS Publishing for details how to configure IIS + ASP.NET Core app.




回答2:


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




回答3:


I found a response by David Fowler to similar issue. Url: https://github.com/aspnet/HttpSysServer/issues/425

With those changes i am able to run in both Visual Studio and from dotnet run command.



来源:https://stackoverflow.com/questions/45877365/dotnet-run-angular-windows-authentication-not-authenticated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!