如何配置 ASP.NET Core 应用程序以使用 Windows 身份验证? [英] How to configure ASP.NET Core application to use windows authentication?

查看:25
本文介绍了如何配置 ASP.NET Core 应用程序以使用 Windows 身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

Windows Auth 是在 program.cs 中的 web 主机配置过程中配置的

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

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

特别是 UseIISIntegration() 行.

Specifically it's the UseIISIntegration() line.

既然自己什么都不做,还需要在aspNetCore节点的web.config中进行配置;

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=".logsstdout" 
    forwardWindowsAuthToken="true" />

需要设置forwardWindowsAuthToken值.

所以不,你不能在 env.IsDevelopment() 检查中做到这一点.

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

这篇关于如何配置 ASP.NET Core 应用程序以使用 Windows 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆