Esempio n. 1
0
        public IdentityToken(IdentityTokenRequest rawToken, string audience, string authMetadataEndpoint)
        {
            X509Certificate2 currentCertificate = null;

            currentCertificate = AuthMetadata.GetSigningCertificate(new Uri(authMetadataEndpoint));

            JsonWebSecurityTokenHandler jsonTokenHandler =
                GetSecurityTokenHandler(audience, authMetadataEndpoint, currentCertificate);

            SecurityToken        jsonToken = jsonTokenHandler.ReadToken(rawToken.token);
            JsonWebSecurityToken webToken  = (JsonWebSecurityToken)jsonToken;

            x5t = currentCertificate.Thumbprint;
            iss = webToken.Issuer;
            aud = webToken.Audience;
            exp = webToken.ValidTo;
            nbf = webToken.ValidFrom;
            foreach (JsonWebTokenClaim claim in webToken.Claims)
            {
                if (claim.ClaimType.Equals(AuthClaimTypes.AppContextSender))
                {
                    appctxsender = claim.Value;
                }

                if (claim.ClaimType.Equals(AuthClaimTypes.IsBrowserHostedApp))
                {
                    isbrowserhostedapp = claim.Value == "true";
                }

                if (claim.ClaimType.Equals(AuthClaimTypes.AppContext))
                {
                    string[] appContextClaims = claim.Value.Split(',');
                    Dictionary <string, string> appContext =
                        new JavaScriptSerializer().Deserialize <Dictionary <string, string> >(claim.Value);
                    amurl     = appContext[AuthClaimTypes.MsExchAuthMetadataUrl];
                    msexchuid = appContext[AuthClaimTypes.MsExchImmutableId];
                    version   = appContext[AuthClaimTypes.MsExchTokenVersion];
                }
            }
        }
        public IdentityToken(IdentityTokenRequest rawToken, string audience, string authMetadataEndpoint)
        {
            X509Certificate2 currentCertificate = null;

            currentCertificate = AuthMetadata.GetSigningCertificate(new Uri(authMetadataEndpoint));

            JsonWebSecurityTokenHandler jsonTokenHandler =
                GetSecurityTokenHandler(audience, authMetadataEndpoint, currentCertificate);

            SecurityToken jsonToken = jsonTokenHandler.ReadToken(rawToken.token);
            JsonWebSecurityToken webToken = (JsonWebSecurityToken)jsonToken;

            x5t = currentCertificate.Thumbprint;
            iss = webToken.Issuer;
            aud = webToken.Audience;
            exp = webToken.ValidTo;
            nbf = webToken.ValidFrom;
            foreach (JsonWebTokenClaim claim in webToken.Claims)
            {
                if (claim.ClaimType.Equals(AuthClaimTypes.AppContextSender))
                {
                    appctxsender = claim.Value;
                }

                if (claim.ClaimType.Equals(AuthClaimTypes.IsBrowserHostedApp))
                {
                    isbrowserhostedapp = claim.Value == "true";
                }

                if (claim.ClaimType.Equals(AuthClaimTypes.AppContext))
                {
                    string[] appContextClaims = claim.Value.Split(',');
                    Dictionary<string, string> appContext =
                        new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(claim.Value);
                    amurl = appContext[AuthClaimTypes.MsExchAuthMetadataUrl];
                    msexchuid = appContext[AuthClaimTypes.MsExchImmutableId];
                    version = appContext[AuthClaimTypes.MsExchTokenVersion];
                }
            }
        }
        public static DecodedJsonToken Decode(IdentityTokenRequest rawToken)
        {
            string[] tokenParts = rawToken.token.Split('.');

            if (tokenParts.Length != 3)
            {
                throw new ApplicationException("Token must have three parts separated by '.' characters.");
            }

            string encodedHeader = tokenParts[0];
            string encodedPayload = tokenParts[1];
            string signature = tokenParts[2];

            string decodedHeader = Base64UrlEncoder.Decode(encodedHeader);
            string decodedPayload = Base64UrlEncoder.Decode(encodedPayload);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Dictionary<string, string> header = serializer.Deserialize<Dictionary<string, string>>(decodedHeader);
            Dictionary<string, string> payload = serializer.Deserialize<Dictionary<string, string>>(decodedPayload);

            return new DecodedJsonToken(header, payload, signature);
        }
        public static DecodedJsonToken Decode(IdentityTokenRequest rawToken)
        {
            string[] tokenParts = rawToken.token.Split('.');

            if (tokenParts.Length != 3)
            {
                throw new ApplicationException("Token must have three parts separated by '.' characters.");
            }

            string encodedHeader  = tokenParts[0];
            string encodedPayload = tokenParts[1];
            string signature      = tokenParts[2];

            string decodedHeader  = Base64UrlEncoder.Decode(encodedHeader);
            string decodedPayload = Base64UrlEncoder.Decode(encodedPayload);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Dictionary <string, string> header  = serializer.Deserialize <Dictionary <string, string> >(decodedHeader);
            Dictionary <string, string> payload = serializer.Deserialize <Dictionary <string, string> >(decodedPayload);

            return(new DecodedJsonToken(header, payload, signature));
        }