예제 #1
0
        // Copied from ACS code
        // This method returns an AsymmetricSignatureFormatter capable of supporting Sha256 signatures.
        private static RSACryptoServiceProvider GetCryptoProviderForSha256(RSACryptoServiceProvider rsaProvider)
        {
            const int PROV_RSA_AES = 24;    // CryptoApi provider type for an RSA provider supporting sha-256 digital signatures

            // On Mono, use the default ProviderType
            if (rsaProvider.CspKeyContainerInfo.ProviderType == PROV_RSA_AES || Mono.IsMono())
            {
                return(rsaProvider);
            }

            CspParameters csp = new CspParameters
            {
                ProviderType     = PROV_RSA_AES,
                KeyContainerName = rsaProvider.CspKeyContainerInfo.KeyContainerName,
                KeyNumber        = (int)rsaProvider.CspKeyContainerInfo.KeyNumber
            };

            if (rsaProvider.CspKeyContainerInfo.MachineKeyStore)
            {
                csp.Flags = CspProviderFlags.UseMachineKeyStore;
            }

            //
            // If UseExistingKey is not specified, the CLR will generate a key for a non-existent group.
            // With this flag, a CryptographicException is thrown instead.
            //
            csp.Flags |= CspProviderFlags.UseExistingKey;
            return(new RSACryptoServiceProvider(csp));
        }
        public static TokenResponse ReadErrorResponse(WebResponse response)
        {
            if (response == null)
            {
                return(new TokenResponse
                {
                    Error = AdalError.ServiceReturnedError,
                    ErrorDescription = AdalErrorMessage.ServiceReturnedError
                });
            }

            Stream responseStream = response.GetResponseStream();

            if (responseStream == null)
            {
                return(new TokenResponse
                {
                    Error = AdalError.Unknown,
                    ErrorDescription = AdalErrorMessage.Unknown
                });
            }

            TokenResponse tokenResponse;

            try
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TokenResponse));
                tokenResponse = ((TokenResponse)serializer.ReadObject(responseStream));

                // Reset stream position to make it possible for application to read WebException body again
                // Not supported in Mono
                if (!Mono.IsMono())
                {
                    responseStream.Position = 0;
                }
            }
            catch (SerializationException)
            {
                // Not supported in Mono
                if (!Mono.IsMono())
                {
                    responseStream.Position = 0;
                }
                tokenResponse = new TokenResponse
                {
                    Error = (((HttpWebResponse)response).StatusCode == HttpStatusCode.ServiceUnavailable) ?
                            AdalError.ServiceUnavailable :
                            AdalError.Unknown,
                    ErrorDescription = HttpHelper.ReadStreamContent(responseStream)
                };
            }

            return(tokenResponse);
        }