@@ -12,26 +12,31 @@ public class IdentityParser:IIdentityParser<ApplicationUser>
1212 {
1313 public ApplicationUser Parse ( IPrincipal principal )
1414 {
15- var user = new ApplicationUser ( ) ;
16- var claims = ( ClaimsPrincipal ) principal ;
15+ // Pattern matching 'is' expression
16+ // assigns "claims" if "principal" is a "ClaimsPrincipal"
17+ if ( principal is ClaimsPrincipal claims )
18+ {
19+ return new ApplicationUser
20+ {
1721
18- user . CardHolderName = ( claims . Claims . Where ( x => x . Type == "card_holder" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "card_holder" ) . Value : "" ;
19- user . CardNumber = ( claims . Claims . Where ( x => x . Type == "card_number" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "card_number" ) . Value : "" ;
20- user . Expiration = ( claims . Claims . Where ( x => x . Type == "card_expiration" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "card_expiration" ) . Value : "" ;
21- user . CardType = ( claims . Claims . Where ( x => x . Type == "missing" ) . Count ( ) > 0 ) ? int . Parse ( claims . Claims . First ( x => x . Type == "missing" ) . Value ) : 0 ;
22- user . City = ( claims . Claims . Where ( x => x . Type == "address_city" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "address_city" ) . Value : "" ;
23- user . Country = ( claims . Claims . Where ( x => x . Type == "address_country" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "address_country" ) . Value : "" ;
24- user . Email = ( claims . Claims . Where ( x => x . Type == "email" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "email" ) . Value : "" ;
25- user . Id = ( claims . Claims . Where ( x => x . Type == "sub" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "sub" ) . Value : "" ;
26- user . LastName = ( claims . Claims . Where ( x => x . Type == "last_name" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "last_name" ) . Value : "" ;
27- user . Name = ( claims . Claims . Where ( x => x . Type == "name" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "name" ) . Value : "" ;
28- user . PhoneNumber = ( claims . Claims . Where ( x => x . Type == "phone_number" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "phone_number" ) . Value : "" ;
29- user . SecurityNumber = ( claims . Claims . Where ( x => x . Type == "card_security_number" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "card_security_number" ) . Value : "" ;
30- user . State = ( claims . Claims . Where ( x => x . Type == "address_state" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "address_state" ) . Value : "" ;
31- user . Street = ( claims . Claims . Where ( x => x . Type == "address_street" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "address_street" ) . Value : "" ;
32- user . ZipCode = ( claims . Claims . Where ( x => x . Type == "address_zip_code" ) . Count ( ) > 0 ) ? claims . Claims . First ( x => x . Type == "address_zip_code" ) . Value : "" ;
33-
34- return user ;
22+ CardHolderName = claims . Claims . FirstOrDefault ( x => x . Type == "card_holder" ) ? . Value ?? "" ,
23+ CardNumber = claims . Claims . FirstOrDefault ( x => x . Type == "card_number" ) ? . Value ?? "" ,
24+ Expiration = claims . Claims . FirstOrDefault ( x => x . Type == "card_expiration" ) ? . Value ?? "" ,
25+ CardType = int . Parse ( claims . Claims . FirstOrDefault ( x => x . Type == "missing" ) ? . Value ?? "0" ) ,
26+ City = claims . Claims . FirstOrDefault ( x => x . Type == "address_city" ) ? . Value ?? "" ,
27+ Country = claims . Claims . FirstOrDefault ( x => x . Type == "address_country" ) ? . Value ?? "" ,
28+ Email = claims . Claims . FirstOrDefault ( x => x . Type == "email" ) ? . Value ?? "" ,
29+ Id = claims . Claims . FirstOrDefault ( x => x . Type == "sub" ) ? . Value ?? "" ,
30+ LastName = claims . Claims . FirstOrDefault ( x => x . Type == "last_name" ) ? . Value ?? "" ,
31+ Name = claims . Claims . FirstOrDefault ( x => x . Type == "name" ) ? . Value ?? "" ,
32+ PhoneNumber = claims . Claims . FirstOrDefault ( x => x . Type == "phone_number" ) ? . Value ?? "" ,
33+ SecurityNumber = claims . Claims . FirstOrDefault ( x => x . Type == "card_security_number" ) ? . Value ?? "" ,
34+ State = claims . Claims . FirstOrDefault ( x => x . Type == "address_state" ) ? . Value ?? "" ,
35+ Street = claims . Claims . FirstOrDefault ( x => x . Type == "address_street" ) ? . Value ?? "" ,
36+ ZipCode = claims . Claims . FirstOrDefault ( x => x . Type == "address_zip_code" ) ? . Value ?? ""
37+ } ;
38+ }
39+ throw new ArgumentException ( message : "The principal must be a ClaimsPrincipal" , paramName : nameof ( principal ) ) ;
3540 }
3641 }
3742}
0 commit comments