asp.net-core - .NET Core 3.1 User.Identity.Name 使用 Windows 身份验证时始终为空

标签 asp.net-core

我在 .net core 3.1 应用程序中使用 Windows 身份验证。我想检索当前用户名,但它始终为空。我还无法将此应用程序部署到 IIS,因此在使用 IISExpress 进行调试时会发生这种情况。

launchSettings.json:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,    
    "iisExpress": {
      "applicationUrl": "http://localhost:52362",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

启动.cs:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(IISDefaults.AuthenticationScheme);
            services.AddAuthorization();

            services.AddHttpContextAccessor();
            services.AddMvc();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }
        
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseAuthentication();
            app.UseRouting();
            app.UseAuthorization();
        }

Controller :

[Authorize]
[ApiController]
[Route("[controller]")]
public class FilingController : Controller
{
    private readonly string _currentUserName;
    public FilingController(IHttpContextAccessor accessor)
    {
        //This is always null
        _currentUserName = accessor.HttpContext.User.Identity.Name;
    }
}

这是 Program.cs:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseIISIntegration();
                    webBuilder.UseIIS();
                });
    }

这发生在成功登录之后。为什么名称为空,如何解决?

最佳答案

看起来可能与 How does HttpContext.Current.User.Identity.Name know which usernames exist? 重复

在 Visual Studio 解决方案资源管理器中 - 单击 Web 应用程序项目 -> 在底部选择“属性”选项卡。确保您具有以下设置:

匿名身份验证 |已禁用

Windows 身份验证 |已启用

我已经在你的代码中看到了你设置的

“iisSettings”:{ “windowsAuthentication”:真实的, “匿名身份验证”:假,

但是执行以下验证步骤以检查是否已根据您已配置的 iisSettings 设置了匿名身份验证和 Windows 身份验证 - 如果没有,那就是问题所在

验证步骤:

  1. 选择您的项目。
  2. 按 F4
  3. 检查“匿名身份验证”是否已禁用以及“Windows 身份验证”是否已启用

关于asp.net-core - .NET Core 3.1 User.Identity.Name 使用 Windows 身份验证时始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64125305/

相关文章:

database - 使用具有一对多关系的支持字段

vue.js - 如何修复 block 加载失败?

c# - ASP.NET Core 本地化 - 不从资源文件返回值,仅返回名称

c# - ASP.NET Core 世界中的即发即弃的 native /规范方法

c# - 使用 ASP.NET Core 3.1,如何从页面 URL 中剥离 Home Controller ?

asp.net-core - 使用 AddApplicationPart 托管 asp.core : how to replace obsolete RazorViewEngineOptions. CompilationCallback?

asp.net-mvc - Asp.Net MVC返回列表查看

c# - 什么等同于 ASP.NET Core 中的 HostingEnvironment.ShutdownReason?

asp.net-core - 点网核心 : What are the sha512 and shaPath properties used for in . deps.json

testing - 如何实现 IdentityServer4 的集成测试?