【问题标题】:.NET Core 3.1 User.Identity.Name always null with Windows Authentication.NET Core 3.1 User.Identity.Name 在 Windows 身份验证中始终为空
【发布时间】:2020-09-29 18:12:44
【问题描述】:

我正在使用带有 .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"
      }
    }
  }
}

Startup.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();
        }

控制器:

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

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

【问题讨论】:

  • 您能否给我们看看您的program.cs 文件,因为我没有重现您的问题。
  • 我在上面的帖子中添加了 Program.cs
  • 我用了和你一样的代码,但是测试可以得到名字,所以建议你新建一个项目重新测试一下。
  • 你是对的。这是因为我需要使用自定义 ClaimsTransformer 来允许基于 Active Directory 组的访问。用户名未复制到新的 ClaimsPrincipal 返回身份。

标签: asp.net-core


【解决方案1】:

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

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

匿名认证 |已停用

Windows 身份验证 |已启用

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

“iis 设置”:{ “windows身份验证”:真, “anonymousAuthentication”:假,

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

验证步骤:

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

【讨论】:

  • 知道为什么在我的 Web 应用项目(Blazor、.NET 5)的属性下看不到这些选项吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 2019-11-28
相关资源
最近更新 更多