diff --git a/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs b/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs index 5773cff4..55cbfdf0 100644 --- a/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs +++ b/server/StrDss.Api/Authentication/KcJwtBearerEvents.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using StrDss.Common; using StrDss.Model; +using StrDss.Model.UserDtos; using StrDss.Service; using StrDss.Service.Bceid; @@ -78,33 +79,38 @@ public override async Task TokenValidated(TokenValidatedContext context) } } - if (user.IdentityProviderNm == StrDssIdProviders.BceidBusiness && (int)(DateTime.UtcNow - user!.UpdDtm).TotalDays > 1) + //await UpdateBceidUserInfo(user); + } + } + + private async Task UpdateBceidUserInfo(UserDto? user) + { + if (user!.IdentityProviderNm == StrDssIdProviders.BceidBusiness && (int)(DateTime.UtcNow - user!.UpdDtm).TotalDays > 1) + { + try { - try + var (error, account) = await _bceid.GetBceidAccountCachedAsync(_currentUser.UserGuid, "", StrDssIdProviders.BceidBusiness, _currentUser.UserGuid, _currentUser.IdentityProviderNm); + + if (account == null) { - var (error, account) = await _bceid.GetBceidAccountCachedAsync(_currentUser.UserGuid, "", StrDssIdProviders.BceidBusiness, _currentUser.UserGuid, _currentUser.IdentityProviderNm); + _logger.LogError($"BCeID call error: {error}"); + } - if (account == null) - { - _logger.LogError($"BCeID call error: {error}"); - } + if (account != null) + { + _currentUser.FirstName = account.FirstName; + _currentUser.LastName = account.LastName; - if (account != null) + if (account.FirstName != user.GivenNm || account.LastName != user.FamilyNm) { - _currentUser.FirstName = account.FirstName; - _currentUser.LastName = account.LastName; - - if (account.FirstName != user.GivenNm || account.LastName != user.FamilyNm) - { - await _userService.UpdateBceidUserInfo(user.UserIdentityId, account.FirstName, account.LastName); - } + await _userService.UpdateBceidUserInfo(user.UserIdentityId, account.FirstName, account.LastName); } } - catch (Exception ex) - { - _logger.LogError($"BCeID Web call failed - {ex.Message}", ex); - _logger.LogInformation("BCeID Web call failed - Skipping UpdateBceidUserInfo "); - } + } + catch (Exception ex) + { + _logger.LogError($"BCeID Web call failed - {ex.Message}", ex); + _logger.LogInformation("BCeID Web call failed - Skipping UpdateBceidUserInfo "); } } }