Beispiel #1
0
        /// <summary>
        /// Attempts to resolve the _securityKeyIdentifier into a securityKey.  If successful, the private _securityKey is set.
        /// Uses the tokenresolver that was passed in, it may be the case a keyIdentifier can
        /// generate a securityKey.  A RSA key can generate a key with just the public part.
        /// </summary>
        /// <returns>void</returns>
        void ResolveKey()
        {
            if (_securityKeyIdentifier == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ski");
            }

            if (_securityKey == null)
            {
                lock (_keyLock)
                {
                    if (_securityKey == null)
                    {
                        if (_securityTokenResolver != null)
                        {
                            for (int i = 0; i < _securityKeyIdentifier.Count; ++i)
                            {
                                if (_securityTokenResolver.TryResolveSecurityKey(_securityKeyIdentifier[i], out _securityKey))
                                {
                                    return;
                                }
                            }
                        }

                        // most likely a public key, do this last
                        if (_securityKeyIdentifier.CanCreateKey)
                        {
                            _securityKey = _securityKeyIdentifier.CreateKey();
                            return;
                        }

                        throw DiagnosticUtility.ExceptionUtility.ThrowHelper(
                                  new SecurityTokenException(SR.GetString(SR.ID2080,
                                                                          _securityTokenResolver == null ? "null" : _securityTokenResolver.ToString(),
                                                                          _securityKeyIdentifier == null ? "null" : _securityKeyIdentifier.ToString())), System.Diagnostics.TraceEventType.Error);
                    }
                }
            }
        }
        internal static void WriteSecurityKeyIdentifier(XmlWriter writer, SecurityKeyIdentifier ski, SecurityTokenSerializer tokenSerializer)
        {
            if (tokenSerializer == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenSerializer", SR.GetString(SR.SamlSerializerRequiresExternalSerializers));
            }

            bool keyWritten = false;

            if (tokenSerializer.CanWriteKeyIdentifier(ski))
            {
                tokenSerializer.WriteKeyIdentifier(writer, ski);
                keyWritten = true;
            }

            if (!keyWritten)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SamlSerializerUnableToWriteSecurityKeyIdentifier, ski.ToString())));
            }
        }
        internal static void WriteSecurityKeyIdentifier(XmlWriter writer, SecurityKeyIdentifier ski, SecurityTokenSerializer tokenSerializer)
        {
            if (tokenSerializer == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenSerializer", System.IdentityModel.SR.GetString("SamlSerializerRequiresExternalSerializers"));
            }
            bool flag = false;

            if (tokenSerializer.CanWriteKeyIdentifier(ski))
            {
                tokenSerializer.WriteKeyIdentifier(writer, ski);
                flag = true;
            }
            if (!flag)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.IdentityModel.SR.GetString("SamlSerializerUnableToWriteSecurityKeyIdentifier", new object[] { ski.ToString() })));
            }
        }