@@ -70,16 +70,33 @@ private async Task<bool> PopulateCurrentUserFromDb(ClaimsPrincipal principal)
7070 {
7171 var isApiClient = false ;
7272 bool . TryParse ( principal . FindFirstValue ( HmcrClaimTypes . KcIsApiClient ) , out isApiClient ) ;
73+ isApiClient = true ;
7374
74- //preferred_username token has a form of "{username}@{directory}".
75- var preferredUsername = isApiClient ? principal . FindFirstValue ( HmcrClaimTypes . KcApiUsername ) : principal . FindFirstValue ( HmcrClaimTypes . KcUsername ) ;
76- var usernames = preferredUsername . Split ( "@" ) ;
77- var username = usernames [ 0 ] . ToUpperInvariant ( ) ;
78- var directory = usernames [ 1 ] . ToUpperInvariant ( ) ;
75+ //preferred_username token has a form of "{guid}@{directory}".
76+ //var preferredUsername = isApiClient ? principal.FindFirstValue(HmcrClaimTypes.KcApiUsername) : principal.FindFirstValue(HmcrClaimTypes.KcUsername);
77+ var preferredUsername = principal . FindFirstValue ( HmcrClaimTypes . KcUsername ) ;
78+ var username = principal . FindFirstValue ( HmcrClaimTypes . KcApiUsername ) ;
7979
80- var userGuidClaim = directory . ToUpperInvariant ( ) == UserTypeDto . IDIR ? HmcrClaimTypes . KcIdirGuid : HmcrClaimTypes . KcBceidGuid ;
81- var userGuid = new Guid ( principal . FindFirstValue ( userGuidClaim ) ) ;
82- var email = principal . FindFirstValue ( ClaimTypes . Email ) . ToUpperInvariant ( ) ;
80+ var directory = "" ;
81+ Guid userGuid = new Guid ( "00000000-0000-0000-0000-000000000000" ) ;
82+ var email = "" ;
83+ if ( preferredUsername . Contains ( "@" ) )
84+ {
85+ directory = preferredUsername . Split ( "@" ) [ 1 ] . ToUpperInvariant ( ) ;
86+ username = principal . FindFirstValue ( HmcrClaimTypes . KcUsername ) . Split ( "@" ) [ 0 ] . ToUpperInvariant ( ) ;
87+ userGuid = new Guid ( Guid . Parse ( username ) . ToString ( ) ) ;
88+ email = principal . FindFirstValue ( ClaimTypes . Email ) ? . ToUpperInvariant ( ) ;
89+
90+ }
91+ else
92+ {
93+
94+ username = principal . FindFirstValue ( HmcrClaimTypes . KcUsername ) . Split ( "@" ) [ 0 ] . ToUpperInvariant ( ) ;
95+ userGuid = new Guid ( principal . FindFirstValue ( "idir_userid" ) ? . ToUpperInvariant ( ) ) ;
96+ email = principal . FindFirstValue ( ClaimTypes . Email ) ? . ToUpperInvariant ( ) ;
97+
98+ }
99+
83100
84101 var user = await _userService . GetActiveUserEntityAsync ( userGuid ) ;
85102 if ( user == null )
@@ -96,7 +113,7 @@ private async Task<bool> PopulateCurrentUserFromDb(ClaimsPrincipal principal)
96113 email = user . Email ;
97114 }
98115
99- if ( directory == "IDIR" )
116+ if ( directory . Equals ( "IDIR" , StringComparison . OrdinalIgnoreCase ) )
100117 {
101118 _curentUser . UserGuid = userGuid ;
102119 _curentUser . UserType = UserTypeDto . INTERNAL ;
@@ -105,8 +122,8 @@ private async Task<bool> PopulateCurrentUserFromDb(ClaimsPrincipal principal)
105122 {
106123 _curentUser . UserGuid = userGuid ;
107124 _curentUser . BusinessGuid = user . BusinessGuid ;
108- _curentUser . BusinessLegalName = user . Party . BusinessLegalName ;
109- _curentUser . BusinessNumber = user . Party . BusinessNumber ?? 0 ;
125+ _curentUser . BusinessLegalName = user . Party ? . BusinessLegalName ;
126+ _curentUser . BusinessNumber = user . Party ? . BusinessNumber ?? 0 ;
110127 _curentUser . UserType = UserTypeDto . BUSINESS ;
111128 }
112129
0 commit comments