代码之家  ›  专栏  ›  技术社区  ›  Muflix

httpContext在带有Omnisharp扩展名的Visual Studio代码中的工作方式不同

  •  0
  • Muflix  · 技术社区  · 6 年前

    我安装了Visual Studio代码并安装了C扩展

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

    当我打开现有 ASP.NET核心2 Project I可以编辑文件并使用调试选项运行Web服务器( Debug > Start Debugging )

    但是我得到了关于httpContext的空异常(来自Visual Studio 2017的调试不会引发此错误)

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

    当我将代码更改为

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

    它起作用了。因此,在我看来,C扩展运行的Web服务器或设置与Visual Studio 2017不同。我有这样的假设,问题可能在 Windows身份验证设置 但我不知道在哪里设置这个?

    有什么想法吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Edward    6 年前

    这与托管有关 Asp.Net Core . 一般来说,启动新项目有三种选择, IIS , IIS Express Project . 从启动时 VS 2017 ,您可以使用 服务器 哪些支持 Windows Authentcation 你可以通过 launchsettings.json .

    但是,从 伊斯 服务器 中不支持 VS Code . 那你就要走了 .NET Core 通过 项目 使用时 VS代码 . 当您使用自己托管的 项目 ,您需要使用 Http.sys 具有 Windows Authencation 支持 Windows Authentication .

    为了解决您的问题,您可以修改 Program.cs 如下所示 HTTPs系统 具有 Windows身份验证 .

    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();