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

查看:262
本文介绍了如何配置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在program.cs

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=".\logs\stdout" 
    forwardWindowsAuthToken="true" />

需要设置 forwardWindowsAuthToken 值。

所以不,您不能在env.IsDevelopment()检查中这样做。

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

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

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