示例#1
0
        internal UserContext(AuthenticationResult authResult)
        {
            if (authResult is null)
            {
                throw new ArgumentNullException();
            }

            AccessToken = authResult.AccessToken;

            if (string.IsNullOrEmpty(AccessToken.Trim()))
            {
                throw new ArgumentException("Unable to create UserContext. Object 'Access Token' in AuthenticationResult is missing or invalid");
            }

            var jwtPayload = authResult.ToJwtPayloadAsJObject();

            if (jwtPayload is null)
            {
                throw new ArgumentException("Unable to create UserContext. Object 'User' now defined in AuthenticationResult");
            }

            IsNewUser      = jwtPayload["newUser"]?.ToString()?.ToLowerInvariant() == "true";
            Scope          = jwtPayload["scp"]?.ToString();
            Name           = jwtPayload["user"]?.ToString();
            UserIdentifier = jwtPayload["sub"]?.ToString();
            GivenName      = jwtPayload["given_name"]?.ToString();
            FamilyName     = jwtPayload["family_name"]?.ToString();
            StreetAddress  = jwtPayload["streetAddress"]?.ToString();
            City           = jwtPayload["city"]?.ToString();
            Province       = jwtPayload["state"]?.ToString();
            PostalCode     = jwtPayload["postalCode"]?.ToString();
            Country        = jwtPayload["country"]?.ToString();
            JobTitle       = jwtPayload["jobTitle"]?.ToString();

            if (double.TryParse(jwtPayload["exp"]?.ToString(), out var expiresEpoch))
            {
                AccessTokenExpires = expiresEpoch.UnixTimeStampToDateTimeUtc();
            }

            EmailAddresses = ((JArray)jwtPayload["emails"])?.Select(emailAddress => emailAddress?.ToString());

            IsLoggedOn = true;
        }