简体   繁体   English

为什么运行 ASP.NET 6 项目时 `User.Identity.Name` `null`?

[英]Why is `User.Identity.Name` `null` when running ASP.NET 6 project?

I'm working on an app that's installed on-premise for our customers, so Windows Authentication is all we need.我正在开发一个为我们的客户本地安装的应用程序,因此我们只需要 Windows 身份验证。 I'm currently updating the app from ASP.NET 4.8 to ASP.NET 6. When I run the app using the IIS Express launch profile, it works as I would expect, but when I run using the 'MyProject' launch profile, User.Identity.Name is null .我目前正在将应用程序从 ASP.NET 4.8 更新到 ASP.NET 6。当我使用 IIS Express 启动配置文件运行应用程序时,它按我预期的方式运行,但是当我使用“MyProject”启动配置文件运行时, User.Identity.Namenull

My launchsettings.json file:我的launchsettings.json文件:

{
"iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
        "applicationUrl": "http://localhost:42721"
    }
},
"profiles": {
    "MyProject": {
        "commandName": "Project",
        "dotnetRunMessages": true,
        "launchBrowser": true,
        "launchUrl": "/",
        "applicationUrl": "http://localhost:42721",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    },
    "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "launchUrl": "/",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
}

} }

My Program.cs file:我的Program.cs文件:

using NLog;
using NLog.Web;
using System.Text.Json;

var logger = LogManager.Setup()
    .LoadConfigurationFromAppSettings()
    .GetCurrentClassLogger();
logger.Debug("init main");
    
var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
        options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    });

builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromSeconds(60);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
});

// NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
builder.Host.UseNLog();

// Add services to the container.

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseDefaultFiles();
app.UseStaticFiles();

app.UseSession();
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
app.MapControllers();

app.Run();

Why does the Windows Authentication work when I'm debugging using IIS Express but not when I'm using the 'MyProject' launch profile?为什么 Windows 身份验证在我使用 IIS Express 进行调试时有效,但在我使用“MyProject”启动配置文件时却无效?

The following rows are important in your Program.cs:以下行在您的 Program.cs 中很重要:

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate(); // which used by microsoft.aspnetcore.authentication.negotiate nuget package
builder.Services.AddAuthorization(options =>
{
    // By default, all incoming requests will be authorized according to the default policy.
    options.FallbackPolicy = options.DefaultPolicy;
});
//after app intialization:
app.UseAuthentication();
app.UseAuthorization();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 User.Identity.Name 是 null 在我的 ASP.NET 核心 Web ZDB974238714CA8A14A7CE1D0 - User.Identity.Name is null in my ASP.NET Core Web API User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null - User.Identity.Name returns null in ASP.NET Core MVC application 对于托管在 asp.net core 中的 Blazor Wasm,User.Identity.Name 为 null - User.Identity.Name is null for Blazor Wasm hosted in asp.net core User.Identity.Name 在 asp.net core + angular 客户端应用程序中为空 - User.Identity.Name is null in asp.net core + angular client app ASP.NET控制器基类User.Identity.Name - ASP.NET Controller Base Class User.Identity.Name 会话与User.Identity.Name进行比较,以使用Windows身份验证检查ASP.net核心应用程序的登录用户名 - Session vs User.Identity.Name to check the loggeduser name for ASP.net core application with windows authentication 测试使用User.Identity.Name的Asp.Net控制器操作方法? - Test an Asp.Net Controller action method which uses User.Identity.Name? ASP.NET Web 表单应用程序 User.Identity.Name 在 IIS 中不起作用 - ASP.NET web form application User.Identity.Name not working in IIS 将参数值传递给SqlDataSource ASP.NET User.Identity.Name - Pass parameter value to SqlDataSource ASP.NET User.Identity.Name 如何在ASP.NET中使用User.Identity.Name作为SqlDataSource的参数? - How to use User.Identity.Name as a parameter for SqlDataSource in ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM