我有一个.net Core 2.1 MVC应用程序,它承载一个Angular 6应用程序.我使用的是Visual Studio 2017 15.7.3.我正在尝试设置 Windows身份验证但遇到问题.我按照文档和我的Program.cs和Startup.cs如下: Progra
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 } }
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" }); }); } } }
如果我取消注释program.cs中的.UseHttpSys部分并按Visual Studio中的play进行调试,它几乎会立即停止调试会话,并且事件日志中出现以下错误:
“Application ‘MACHINE/WEBROOT/APPHOST/myapp’ with physical root
‘C:\Users\me\myapp’ created process with commandline ‘C:\Program Files
(x86)\Microsoft Visual
Studio\2017\Professional\Common7\IDE\Extensions\Microsoft\Web
Tools\ProjectSystem\VSIISExeLauncher.exe -argFile
“C:\Users\me\AppData\Local\Temp\tmpFCA6.tmp”‘ but failed to listen on
the given port ‘28353’”.
如果我尝试使用.UseHttpSys注释掉,调试工作但我无法正确验证.
卡米洛感谢您的评论,这有助于我指出正确的方向.我从program.cs中删除了UseHttpSys部分并添加了.UseIISIntegration().我还在startup.cs中将services.AddAuthentication(HttpSysDefaults.AuthenticationScheme)更改为services.AddAuthentication(IISDefaults.AuthenticationScheme).然后,我在项目属性的“调试”部分选中“启用Windows身份验证”,并为“启动”选择“IIS Express”,这一切都有效.我是新的.net核心与Windows身份验证,所以我不知道我是否所有设置完全正确,但它的工作,希望,这篇文章有助于指出其他人正确的方向.