.net Core 2.1应用程序中的Windows身份验证

Lan*_*ceM 4 c# windows-authentication asp.net-core

我有一个.net Core 2.1 MVC应用程序,它承载一个Angular 6应用程序.我使用的是Visual Studio 2017 15.7.3.我正在尝试设置Windows身份验证但遇到问题.我按照文档和我的Program.cs和Startup.cs如下:

Program.cs中:

using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Web;
using System;

namespace CoreIV_Admin_Core
{
public class Program
{
    public static void Main(string[] args)
    {
        var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

        try
        {
            logger.Debug("init main");
            CreateWebHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            //NLog: catch setup errors
            logger.Error(ex, "Stopped program because of exception");
            throw;
        }
        finally
        {
            // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
            NLog.LogManager.Shutdown();
        }
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            //.UseHttpSys(options =>
            //{
            //    options.Authentication.Schemes = AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
            //    options.Authentication.AllowAnonymous = false;
            //})
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
            })
            .UseNLog(); // NLog: setup NLog for Dependency injection
}
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace CoreIV_Admin_Core
{
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration
    {
        get;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddAuthentication(HttpSysDefaults.AuthenticationScheme);

        services.AddMvc()
               .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
            {
                HotModuleReplacement = true
            });
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.UseStaticFiles();

        app.UseCookiePolicy();

        app.UseHttpContext();

        app.UseAuthentication();

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

            routes.MapSpaFallbackRoute(
                name: "spa-fallback",
                defaults: new
                {
                    controller = "Home",
                    action = "Index"
                });
        });
    }
 }
}
Run Code Online (Sandbox Code Playgroud)

如果我取消注释.UseHttpSysprogram.cs中的部分并按Visual Studio中的play进行调试,它几乎会立即停止调试会话,并且事件日志中出现以下错误:

"应用'MACHINE/WEBROOT/APPHOST/myapp'与物理根'C:\ Users\me\myapp'创建进程与命令行'C:\ Program Files(x86)\ Microsoft Visual Studio\2017\Professional\Common7\IDE \扩展\ Microsoft\Web Tools\ProjectSystem\VSIISExeLauncher.exe -argFile"C:\ Users\me\AppData\Local\Temp\tmpFCA6.tmp"'但未能侦听给定端口'28353'".

如果我尝试.UseHttpSys注释掉,调试工作,但我无法正确验证.

Lan*_*ceM 9

卡米洛感谢您的评论,这有助于我指出正确的方向.我从program.cs中删除了UseHttpSys部分并添加了.UseIISIntegration().我也改变了services.AddAuthentication(HttpSysDefaults.AuthenticationScheme)services.AddAuthentication(IISDefaults.AuthenticationScheme)我的startup.cs.然后Enable Windows Authentication,我在项目属性的Debug部分中勾选,并选择"IIS Express"进行启动,这一切都奏效了.我是新的.net核心与Windows身份验证,所以我不知道我是否所有设置完全正确,但它的工作,希望,这篇文章有助于指出其他人正确的方向.