简体   繁体   English

在 ASP .NET Core 6.0 中获取 Windows 用户名

[英]Getting the Windows User Name in ASP .NET Core 6.0

I am currently trying to migrate an ASP.NET Core 5.0 project to ASP.NET Core 6.0.我目前正在尝试将 ASP.NET Core 5.0 项目迁移到 ASP.NET Core 6.0。 The Window user name is showing up on.NET 5.0. Window 用户名显示在 .NET 5.0 上。 However, for .NET 6 project Window username is always using anonymous user.但是,对于 .NET 6 项目,Window 用户名始终使用匿名用户。 I am not sure if I am missing any code.我不确定我是否缺少任何代码。 Any help/suggestion is welcome!欢迎任何帮助/建议!

.NET CORE 6.0 Window User .NET CORE 6.0 Window User .NET CORE 6.0 窗口用户.NET CORE 6.0 窗口用户

Old .NET CORE 5.0 Window User .NET CORE 5.0 Window User旧 .NET CORE 5.0 窗口用户.NET CORE 5.0 窗口用户

launchSettings.json

"iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:55305",
      "sslPort": 44383
    }
  }
Program.cs

using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.AspNetCore.Authentication.Negotiate;

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.

builder.Services.AddControllersWithViews();
builder.Services.AddHttpContextAccessor();

builder.Services.AddSession(options => {
    options.IdleTimeout = TimeSpan.FromMinutes(120);//You can set Time   
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/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.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseSession();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=index}/{id?}");

app.Run();
HomeController.cs

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IHttpContextAccessor? _httpContextAccessor;

        public HomeController(ILogger<HomeController> logger, IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;
            _httpContextAccessor = httpContextAccessor;
            string CurrentUserName = httpContextAccessor.HttpContext?.User.Identity?.Name;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

The reason for this problem is because you are missing this line of code.这个问题的原因是因为你缺少这行代码。

// pls add this line
app.UseAuthentication();
app.UseAuthorization();

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

相关问题 会话与User.Identity.Name进行比较,以使用Windows身份验证检查ASP.net核心应用程序的登录用户名 - Session vs User.Identity.Name to check the loggeduser name for ASP.net core application with windows authentication Asp net Core 获取用户 Windows 用户名 - Asp net Core Get user Windows username Asp.Net Core 2.1 Windows Auth。 HttpContext.User.Identity.Name在IIS中不起作用 - Asp.Net Core 2.1 Windows Auth. HttpContext.User.Identity.Name not working in IIS ASP.NET Core 6.0 计数 - ASP.NET Core 6.0 Counting asp.net 核心 6.0 kestrel 服务器不工作 - asp net core 6.0 kestrel server is not working 如何使用 ASP.NET Core 6.0 和 Angular 项目配置 Windows 身份验证 - How to configure Windows authentication with ASP.NET Core 6.0 and Angular project ASP.NET Core 6.0+: 如何获取当前(区分大小写)IIS 网站名称? - ASP.NET Core 6.0+: How to get the current (case-sensitive) IIS Website Name? 使用 ASP.NET Core 6.0 中的中间件将 Auth0 用户 ID 传递到服务层 Web API - Passing Auth0 User Id to service layer using middelware in an ASP.NET Core 6.0 Web API 如何在 C# ASP.NET Core 6.0 中从 Azure AD 检索用户拥有的所有组? - How to retrieve all the groups that a user has from Azure AD in C# ASP.NET Core 6.0? 获取Windows用户名,而不是以ASP .NET Web窗体4.5登录用户名 - getting windows username instead of logged in user name in asp .net web form 4.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM