【问题标题】:How to configure ASP.NET Core application to use windows authentication?如何配置 ASP.NET Core 应用程序以使用 Windows 身份验证?
【发布时间】:2016-10-12 21:52:51
【问题描述】:

我想将 ASP.NET 应用程序配置为根据环境使用不同的身份验证。因此,对于开发环境,我想使用 Windows 身份验证,而对于所有其他环境,我想使用 Facebook 身份验证。

我已经为非开发环境配置了 Facebook 身份验证。如何为开发环境配置 Windows 身份验证? Windows 身份验证仅在开发过程中使用,因此开发人员不必每次在 VS 中运行应用程序时都必须登录。我们有多个开发人员,这意味着 Windows 身份将根据执行者的不同而有所不同。创建 windows 身份后,我将向 windows 身份添加声明。

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

【问题讨论】:

    标签: asp.net-core .net-core coreclr


    【解决方案1】:

    Windows Auth 是在 program.cs 中的 Web 主机配置期间配置的

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

    特别是 UseIISIntegration() 行。

    现在它自己什么都不做,还需要在aspNetCore节点的web.config中配置;

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

    forwardWindowsAuthToken 值需要设置。

    所以不,您不能在 env.IsDevelopment() 检查中执行此操作。

    【讨论】:

      【解决方案2】:

      如果您将 IIS Express 用于开发环境,则有一种快速设置 Windows 身份验证的方法。在Properties文件夹中有一个launchSettings.json,你可以很方便的修改它使用Windows认证进行开发,而不需要修改Startup类。

      在此文件中,您可以将“windowsAuthentication”更改为 true,将“anonymousAuthentication”更改为 false。

      这里是示例launchSettings.json

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

      修改后,您可以通过选择 IIS Express 作为调试目标来运行应用程序。

      【讨论】:

        猜你喜欢
        • 2017-08-31
        • 2011-06-27
        • 2014-08-03
        • 2018-08-15
        • 2019-06-30
        • 2018-07-13
        • 1970-01-01
        • 1970-01-01
        • 2017-02-14
        相关资源
        最近更新 更多