Exemple #1
0
        public static IdToken Parse(string idToken)
        {
            if (string.IsNullOrEmpty(idToken))
            {
                return(null);
            }

            string[] idTokenSegments = idToken.Split(new[] { '.' });

            if (idTokenSegments.Length < 2)
            {
                throw new MsalClientException(
                          MsalError.InvalidJwtError,
                          MsalErrorMessage.IDTokenMustHaveTwoParts);
            }

            try
            {
                string payload       = Base64UrlHelpers.DecodeToString(idTokenSegments[1]);
                var    idTokenClaims = JsonConvert.DeserializeObject <Dictionary <string, object> >(payload);

                IdToken parsedIdToken = new IdToken();

                List <Claim> claims = GetClaimsFromRawToken(idTokenClaims);
                parsedIdToken.ClaimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims));

                parsedIdToken.ObjectId          = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.ObjectId)?.Value;
                parsedIdToken.Subject           = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.Subject)?.Value;
                parsedIdToken.TenantId          = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.TenantId)?.Value;
                parsedIdToken.PreferredUsername = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.PreferredUsername)?.Value;
                parsedIdToken.Name       = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.Name)?.Value;
                parsedIdToken.Upn        = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.Upn)?.Value;
                parsedIdToken.GivenName  = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.GivenName)?.Value;
                parsedIdToken.FamilyName = parsedIdToken.ClaimsPrincipal.FindFirst(IdTokenClaim.FamilyName)?.Value;

                return(parsedIdToken);
            }
            catch (JsonException exc)
            {
                throw new MsalClientException(
                          MsalError.JsonParseError,
                          MsalErrorMessage.FailedToParseIDToken,
                          exc);
            }
        }
Exemple #2
0
        public static ClientInfo CreateFromEncodedString(string encodedUserIdentiier)
        {
            if (string.IsNullOrEmpty(encodedUserIdentiier))
            {
                return(null);
            }

            string[] artifacts = encodedUserIdentiier.Split('.');

            if (artifacts.Length == 0)
            {
                return(null);
            }

            return(new ClientInfo()
            {
                UniqueIdentifier = Base64UrlHelpers.DecodeToString(artifacts[0]),
                UniqueTenantIdentifier = Base64UrlHelpers.DecodeToString(artifacts[1]),
            });
        }