2

I am currently trying to migrate an ASP.NET Core 5.0 project to ASP.NET Core 6.0. The Window user name is showing up on.NET 5.0. However, for .NET 6 project Window username is always using anonymous user. 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

Old .NET CORE 5.0 Window User .NET CORE 5.0 Window User

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 });
        }
    }
}
uncake77
  • 21
  • 1
  • 2
  • 1
    Have you tried using the request context? `Request.RequestContext.HttpContext.User.Identity.Name` This is from a project that is not Core, figured its worth a shot though. Also in my web.config, in the system.web node i have . Perhaps there is something similar in core? – hijinxbassist May 19 '22 at 17:25
  • 1
    The Request is null. Also RequestContext is deprecated in .net 6 – uncake77 May 19 '22 at 18:04
  • What if you're building a razor page web app (no controllers because no MVC)? – LeSteelBox Jun 16 '22 at 16:49

1 Answers1

0

The reason for this problem is because you are missing this line of code.

// pls add this line
app.UseAuthentication();
app.UseAuthorization();
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • 2
    I added the line. The CurrentUserName is still null. Perhaps additional line probably needed? – uncake77 May 23 '22 at 13:36
  • 1
    @uncake77 could you show me the code about this code,let me check the order. – Jason Pan May 23 '22 at 13:47
  • 1
    @uncake77 it should be like my sample code which I have updated just know – Jason Pan May 23 '22 at 13:49
  • 1
    See the Program.cs above, I added app.UseAuthentication() above the app.UseAuthorization(). You can repeat the same behavior, e.g. create a brand new project ASP.net core web app (Model-View-Controller) with .NET 6. Just copy the above code – uncake77 May 23 '22 at 21:47
  • 1
    Set a breakpoint at CurrUserName. You should see the same behavior. – uncake77 May 23 '22 at 21:47
  • Can you try another computer? Or deploy it to your server ? – Jason Pan May 24 '22 at 02:52