

null, I have to assume you're not correctly configuring SignalR to use your ClaimsPrincipalContext.User.IdentifierCustomUserIdProviderContext.UserIdentifierCustomerUserIdProvider public async Task SetUserIdentifier(string userIdentifier)
{
try
{
Console.WriteLine($"Setting user identifier to {userIdentifier}");
var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.UserName == userIdentifier);
if (user == null)
{
user = new User { UserName = userIdentifier };
await _dbContext.Users.AddAsync(user);
await _dbContext.SaveChangesAsync();
}
await Groups.RemoveFromGroupAsync(Context.ConnectionId, user.Id);
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id);
await Clients.Caller.SendAsync("UserIdentifierSet", user.Id);
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}builder.Services.AddDbContext<ApplicationDBContext>(options =>
{
options.UseInMemoryDatabase(databaseName: "MyDatabase");
});
builder.Services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDBContext>()
.AddDefaultTokenProviders();
builder.Services.AddSingleton<IUserIdProvider, CustomUserIdProvider>(); public string GetUserId(HubConnectionContext connection)
{
// Get the authenticated user's ID from the ClaimsPrincipal
var userIdClaim = connection.User?.FindFirst(ClaimTypes.NameIdentifier);
if (userIdClaim != null)
{
return userIdClaim.Value;
}
// If the user is not authenticated or the NameIdentifier claim is not present, return null
return null;
} public async Task SetUserIdentifier(string userIdentifier)
{
try
{
Console.WriteLine($"Setting user identifier to {userIdentifier}");
var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.UserName == userIdentifier);
if (user == null)
{
user = new User { UserName = userIdentifier };
await _dbContext.Users.AddAsync(user);
await _dbContext.SaveChangesAsync();
}
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, user.UserName)
};
var userIdentity = new ClaimsIdentity(claims, "login");
var userPrincipal = new ClaimsPrincipal(userIdentity);
await Groups.RemoveFromGroupAsync(Context.ConnectionId, user.Id);
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id);
await Clients.Caller.SendAsync("UserIdentifierSet", user.Id);
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}