HttpContext在Visual Studio Code和OmniSharp扩展中的工作方式不同

问题描述 投票:0回答:1

我安装了Visual Studio Code并安装了C#Extension

https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp

当我打开现有的Asp.Net Core 2项目时,我可以编辑文件并使用调试选项运行Web服务器(Debug > Start Debugging

但后来我得到了关于HttpContext的null异常(来自visual studio 2017的调试不会抛出此错误)

@User.Identity.Name.Replace(@"Domain\", string.Empty)

当我将代码更改为

@System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

有用。因此在我看来,C#Extension运行不同的Web服务器或使用与Visual Studio 2017不同的设置。我假设问题可以在Windows身份验证设置中,但我不知道在哪里设置它?

任何的想法 ?

c# visual-studio-code asp.net-core-mvc
1个回答
1
投票

这与托管Asp.Net Core有关。一般来说,启动新项目有三种选择:IISIIS ExpressProject。从VS 2017发射时,你可以使用支持IIS ExpressWindows Authentcation,你可以用launchsettings.json配置它。

但是,IIS不支持从IIS ExpressVS Code发射。然后,当使用.NET Core时,你将使用Project lauch VS Code。当你使用Project自己托管时,你需要使用Http.sysWindows Authencation来支持Windows Authentication

为了解决您的问题,您可以像下面一样修改Program.cs以使用Http.sysWindows Authentcation

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseHttpSys(options => {
                    options.Authentication.Schemes =
                        AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
                        options.Authentication.AllowAnonymous = false;
                })
                .Build();
© www.soinside.com 2019 - 2024. All rights reserved.