繁体   English   中英

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

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

我正在开发一个为我们的客户本地安装的应用程序,因此我们只需要 Windows 身份验证。 我目前正在将应用程序从 ASP.NET 4.8 更新到 ASP.NET 6。当我使用 IIS Express 启动配置文件运行应用程序时,它按我预期的方式运行,但是当我使用“MyProject”启动配置文件运行时, User.Identity.Namenull

我的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"
        }
    }
}

}

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

为什么 Windows 身份验证在我使用 IIS Express 进行调试时有效,但在我使用“MyProject”启动配置文件时却无效?

以下行在您的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM