How to configure ASP.NET Core application to use w

2019-06-13 18:56发布

问题:

I want to configure ASP.NET application to use different authentication depends on the environment. So for development environment I want to use Windows authentication and for all other environment I want to use Facebook authentication.

I have already configured Facebook authentication for non development environment. How do I configure windows authentication for development environment? Windows authentication will only be used during development so developers does not have to login every time they run application in VS. We have multiple developers that means the windows identity will be different depend on who is executing it. Once the windows identity is created I will add claims to windows identity.

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        // some stuff here for building configuration
    }

    public IConfigurationRoot Configuration { get; }

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

   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
   {            
        if(env.IsDevelopment())
        {
            // How do i use windows authentication here
        }
        else
        {
                            // this is my custom extension method
            app.UseFacebookAuthentication();
        }

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });            
    }
}

回答1:

Windows Auth is configured during the web host configuration in program.cs

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .Build();

Specifically it's the UseIISIntegration() line.

Now that on it's own does nothing, it also needs configuring in web.config in the aspNetCore node;

<aspNetCore 
    processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" 
    stdoutLogEnabled="false" 
    stdoutLogFile=".\logs\stdout" 
    forwardWindowsAuthToken="true" />

The forwardWindowsAuthToken value needs to be set.

So no, you can't do it within an env.IsDevelopment() check.



回答2:

If you use IIS Express for development environment, there is a quick way of setting up Windows authentication. There is a launchSettings.json in the Properties folder, and you can easily modify it to use Windows authentication for development without modifying the class Startup.

In this file, you can change "windowsAuthentication" to true, and "anonymousAuthentication" to false.

Here is the sample launchSettings.json:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:6366/",
      "sslPort": 0
    }
  },
  profiles": {
   "IIS Express": {...
}

After this modification, you can run the application by selecting IIS Express as the debug target.