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.Negotiatbuilder.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 IHttpContextAccessorbuilder.Services.AddHttpContextAccessor();//injectsbuilder.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.
EDIT:This link under this sectionCircuit handler to capture users for custom services worked when i simply replaced my UserService with it.https://learn.microsoft.com/en-us/aspnet/core/blazor/security/server/additional-scenarios?view=aspnetcore-8.0#set-the-authentication-scheme
interesting things are when@rendermode @(new InteractiveServerRenderMode(false))is not included, your user name will be null. So if you have an SSRT page then this process won't get your currentUser which is interesting.
For others learning ASP.net core the microsoft docs probably have the answer somewhere but overwhelming when you don't know what you are looking for. I had blinders looking for http context fixes and passed over that page several times thinking it isn't what I want. I spent a lot of time on trying to learn hub connections and trying to implement .net 6 server side workarounds