diff --git a/Linguard/Web/Program.cs b/Linguard/Web/Program.cs
index 4b80f9e..1d9e01e 100644
--- a/Linguard/Web/Program.cs
+++ b/Linguard/Web/Program.cs
@@ -42,6 +42,7 @@
#region Web services
builder.Services.AddSingleton
();
+builder.Services.AddSingleton();
builder.Services.AddTransient();
builder.Services.AddTransient();
builder.Services.AddTransient();
@@ -61,7 +62,7 @@
#region Authentication
builder.Services.AddDbContext();
-builder.Services.AddTransient();
+builder.Services.AddScoped();
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentityCore(options => {
options.SignIn.RequireConfirmedAccount = false;
@@ -69,6 +70,7 @@
options.Password.RequiredLength = 1;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
+ options.Password.RequireLowercase = false;
})
.AddRoles()
.AddEntityFrameworkStores()
diff --git a/Linguard/Web/Services/IStateHasChangedNotifierService.cs b/Linguard/Web/Services/IStateHasChangedNotifierService.cs
new file mode 100644
index 0000000..ce6ae13
--- /dev/null
+++ b/Linguard/Web/Services/IStateHasChangedNotifierService.cs
@@ -0,0 +1,7 @@
+namespace Linguard.Web.Services;
+
+public interface IStateHasChangedNotifierService {
+ void Subscribe(EventHandler handler);
+ void UnSubscribe(EventHandler handler);
+ void Notify();
+}
\ No newline at end of file
diff --git a/Linguard/Web/Services/StateHasChangedNotifierService.cs b/Linguard/Web/Services/StateHasChangedNotifierService.cs
new file mode 100644
index 0000000..e858e31
--- /dev/null
+++ b/Linguard/Web/Services/StateHasChangedNotifierService.cs
@@ -0,0 +1,18 @@
+namespace Linguard.Web.Services;
+
+public class StateHasChangedNotifierService : IStateHasChangedNotifierService {
+ private readonly ISet _handlers = new HashSet();
+ public void Subscribe(EventHandler handler) {
+ _handlers.Add(handler);
+ }
+
+ public void UnSubscribe(EventHandler handler) {
+ _handlers.Remove(handler);
+ }
+
+ public void Notify() {
+ foreach (var handler in _handlers) {
+ handler.Invoke(this, EventArgs.Empty);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Linguard/Web/Shared/ProfileMenu.razor b/Linguard/Web/Shared/ProfileMenu.razor
index 73d0ff1..e31fa59 100644
--- a/Linguard/Web/Shared/ProfileMenu.razor
+++ b/Linguard/Web/Shared/ProfileMenu.razor
@@ -27,12 +27,14 @@
@inject AuthenticationStateProvider _authenticationStateProvider
@inject IJSRuntime _jsRuntime
@inject IAuthenticationService _authenticationService
+@inject UserManager _userManager
+@inject IStateHasChangedNotifierService _notifier
@code {
- string? _user;
+ IdentityUser? _user;
string? _email;
- string GreetingMessage => $"Hi, {_user}";
+ string GreetingMessage => $"Hi, {_user?.UserName}";
string GravatarEmail => _email ?? "user@example.com";
async Task OnStyleChanged(string value) {
@@ -44,8 +46,12 @@
protected override async Task OnInitializedAsync() {
await base.OnInitializedAsync();
- var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
- _user = state.User.Identity?.Name;
+ var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
+ var userId = authState.User.Claims.SingleOrDefault(c => c.Type.Equals(ClaimTypes.NameIdentifier));
+ _user = await _userManager.FindByIdAsync(userId?.Value);
+ _notifier.Subscribe((_, _) => {
+ StateHasChanged();
+ });
}
private void Logout() {
diff --git a/Linguard/WebMock/Program.cs b/Linguard/WebMock/Program.cs
index 39c1fee..8dfe9af 100644
--- a/Linguard/WebMock/Program.cs
+++ b/Linguard/WebMock/Program.cs
@@ -64,6 +64,7 @@
#region Web services
builder.Services.AddSingleton();
+builder.Services.AddSingleton();
builder.Services.AddTransient();
builder.Services.AddTransient();
builder.Services.AddScoped();