diff --git a/src/AuthenticationWithClientSideBlazor.Client/ApiAuthenticationStateProvider.cs b/src/AuthenticationWithClientSideBlazor.Client/ApiAuthenticationStateProvider.cs index 1eea73d..2a760bc 100644 --- a/src/AuthenticationWithClientSideBlazor.Client/ApiAuthenticationStateProvider.cs +++ b/src/AuthenticationWithClientSideBlazor.Client/ApiAuthenticationStateProvider.cs @@ -35,10 +35,10 @@ public override async Task GetAuthenticationStateAsync() return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(savedToken), "jwt"))); } - public void MarkUserAsAuthenticated(string email) - { - var authenticatedUser = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, email) }, "apiauth")); - var authState = Task.FromResult(new AuthenticationState(authenticatedUser)); + public void MarkUserAsAuthenticated(string token) + { + var authenticatedUser = new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(token), "jwt")); + var authState = Task.FromResult(new AuthenticationState(authenticatedUser)); NotifyAuthenticationStateChanged(authState); } @@ -49,38 +49,32 @@ public void MarkUserAsLoggedOut() NotifyAuthenticationStateChanged(authState); } - private IEnumerable ParseClaimsFromJwt(string jwt) - { - var claims = new List(); - var payload = jwt.Split('.')[1]; - var jsonBytes = ParseBase64WithoutPadding(payload); - var keyValuePairs = JsonSerializer.Deserialize>(jsonBytes); - - keyValuePairs.TryGetValue(ClaimTypes.Role, out object roles); - - if (roles != null) - { - if (roles.ToString().Trim().StartsWith("[")) - { - var parsedRoles = JsonSerializer.Deserialize(roles.ToString()); - - foreach (var parsedRole in parsedRoles) - { - claims.Add(new Claim(ClaimTypes.Role, parsedRole)); - } - } - else - { - claims.Add(new Claim(ClaimTypes.Role, roles.ToString())); - } - - keyValuePairs.Remove(ClaimTypes.Role); - } - - claims.AddRange(keyValuePairs.Select(kvp => new Claim(kvp.Key, kvp.Value.ToString()))); - - return claims; - } + private IEnumerable ParseClaimsFromJwt(string jwtToken) + { + string[] tokenSegments = jwtToken.Split('.'); + if (tokenSegments.Length != 3) + throw new ArgumentException("JWT Token should have three segments"); + + var payload = tokenSegments[1]; + var jsonBytes = ParseBase64WithoutPadding(payload); + var keyValuePairs = JsonSerializer.Deserialize>(jsonBytes); + + var claims = new List(); + if ((keyValuePairs.TryGetValue(ClaimTypes.Role, out object roles)) && (roles != null) && (roles is JsonElement)) + { + JsonElement jsonRoles = (JsonElement)roles; + if (jsonRoles.ValueKind == JsonValueKind.Array) + claims.AddRange(jsonRoles.EnumerateArray().Select(role => new Claim(ClaimTypes.Role, role.GetString()))); + else + claims.Add(new Claim(ClaimTypes.Role, roles.ToString())); + + // remove the roles claim from the + keyValuePairs.Remove(ClaimTypes.Role); + } + //add other claims as well + claims.AddRange(keyValuePairs.Select(kvp => new Claim(kvp.Key, kvp.Value.ToString()))); + return claims; + } private byte[] ParseBase64WithoutPadding(string base64) { diff --git a/src/AuthenticationWithClientSideBlazor.Client/Services/AuthService.cs b/src/AuthenticationWithClientSideBlazor.Client/Services/AuthService.cs index f8fe120..a83f5d1 100644 --- a/src/AuthenticationWithClientSideBlazor.Client/Services/AuthService.cs +++ b/src/AuthenticationWithClientSideBlazor.Client/Services/AuthService.cs @@ -36,7 +36,7 @@ public async Task Login(LoginModel loginModel) if (result.Successful) { await _localStorage.SetItemAsync("authToken", result.Token); - ((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsAuthenticated(loginModel.Email); + ((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsAuthenticated(result.Token); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", result.Token); return result; diff --git a/src/AuthenticationWithClientSideBlazor.Server/AuthenticationWithClientSideBlazor.Server.csproj b/src/AuthenticationWithClientSideBlazor.Server/AuthenticationWithClientSideBlazor.Server.csproj index 0585115..4e4135e 100644 --- a/src/AuthenticationWithClientSideBlazor.Server/AuthenticationWithClientSideBlazor.Server.csproj +++ b/src/AuthenticationWithClientSideBlazor.Server/AuthenticationWithClientSideBlazor.Server.csproj @@ -6,17 +6,20 @@ - - + + - - - - - - - - + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/src/AuthenticationWithClientSideBlazor.Shared/AuthenticationWithClientSideBlazor.Shared.csproj b/src/AuthenticationWithClientSideBlazor.Shared/AuthenticationWithClientSideBlazor.Shared.csproj index 213a63b..75617be 100644 --- a/src/AuthenticationWithClientSideBlazor.Shared/AuthenticationWithClientSideBlazor.Shared.csproj +++ b/src/AuthenticationWithClientSideBlazor.Shared/AuthenticationWithClientSideBlazor.Shared.csproj @@ -6,7 +6,7 @@ - +