Access Windows User Name with Http Context on Blazor Web App in .NET 8

My current problem when I have a page set to InteractiveServer as the render mode, I can not get access to httpContext.User.Identity.Name as only SSR allows access to httpContext. I have found some explanations for workarounds in .net 6 blazor server app that I have struggled to process and apply to .net 8 blazor web app I am on.

I would like to be able to inject a service for username as AddScoped so each new session can have a distinct Windows user name for processing what they should have access to.

Where in blazor .net 8 is start up first render so I can create the Http context at that instant and have it never be overwritten as null once singalR takes over communication?

I hope this is clear as this is my first endeavor into blazor and ASP.net core as a whole. I am attempting to get my workplace to move off of aspx webforms into a more modern practice.

Let me know if my goal makes sense or if there is better ways to access Windows Authentication.

Break down:

I have enabled windows authentication on IIS.

In my program.cs I have added

//For windows authentication 
//---------------------
//First add nuget package Microsoft.AspNetCore.Authentication.Negotiat
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});
///
app.UseAuthentication();
app.UseAuthorization();
///

I inject a service that is meant to pass the Windows user name to the pages I would like to have it on

//Inject IHttpContextAccessor
builder.Services.AddHttpContextAccessor();
//injects
builder.Services.AddScoped<UserService>();
//

Here is my User service

public class UserService
{
    //Properties
    public User CurrentUser { get; set; }

    private readonly IHttpContextAccessor _httpContextAccessor;

    //Constructors
    public UserService(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
        CurrentUser = new User(GetWindowsUserName());
    }


    //Methods

    /// <summary>
    /// Only supported on Windows platforms
    /// Gets the current windows login of the user
    /// </summary>
    /// <returns>Bool base on if we successfully got user name</returns>
    [System.Runtime.Versioning.SupportedOSPlatform("windows")]
    private string GetWindowsUserName()
    {
        string? userName = null;

        try
        {
            var httpContext = _httpContextAccessor.HttpContext;
            if (httpContext != null && httpContext.User.Identity.IsAuthenticated)
            {
                userName = httpContext.User.Identity.Name;
                userName = userName?.Remove(0, 4);
            }

        }
        catch
        {
            userName = "";
        }

        return userName;
    }
}

on my @page

//RENDER MODE CREATING HTTP ISSUE
@rendermode @(new InteractiveServerRenderMode(false))


@code
{
[Inject] public required UserService UserService { get; set; }
protected override void OnInitialized()
{
    _userName = (UserService.CurrentUser.UserName == null) ?
        "empty" : UserService.CurrentUser.UserName;

}
}

The issue is here with AddScoped for userService when render mode is interactive httpcontext will be null in my user service. If I use add singleton I can get access to the user service info but it will be the same for all my users based on the first user.

New contributor

Quinn Nash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật