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.
Quinn Nash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.