Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="X509CertificateCredentials"/> class.
        /// </summary>
        /// <remarks>The X509Certificate2 argument should have private key in order to sign the message.</remarks>
        /// <param name="certificate">The X509Certificate2 object.</param>
        public X509CertificateCredentials(X509Certificate2 certificate)
            : base(null, true)
        {
            EwsUtilities.ValidateParam(certificate, "certificate");

            if (!certificate.HasPrivateKey)
            {
                throw new ServiceValidationException(Strings.CertificateHasNoPrivateKey);
            }

            this.certificate = certificate;

            string certId = WSSecurityUtilityIdSignedXml.GetUniqueId();

            this.SecurityToken = string.Format(
                X509CertificateCredentials.BinarySecurityTokenFormat,
                certId,
                Convert.ToBase64String(this.certificate.GetRawCertData()));

            SafeXmlDocument doc = new SafeXmlDocument();

            doc.PreserveWhitespace = true;
            doc.LoadXml(string.Format(X509CertificateCredentials.KeyInfoClauseFormat, certId));
            this.keyInfoClause = new KeyInfoNode(doc.DocumentElement);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PartnerTokenCredentials"/> class.
        /// </summary>
        /// <param name="securityToken">The token.</param>
        /// <param name="securityTokenReference">The token reference.</param>
        internal PartnerTokenCredentials(string securityToken, string securityTokenReference)
            : base(securityToken, true /* addTimestamp */)
        {
            EwsUtilities.ValidateParam(securityToken, "securityToken");
            EwsUtilities.ValidateParam(securityTokenReference, "securityTokenReference");

            SafeXmlDocument doc = new SafeXmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(securityTokenReference);
            this.keyInfoNode = new KeyInfoNode(doc.DocumentElement);
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PartnerTokenCredentials"/> class.
        /// </summary>
        /// <param name="securityToken">The token.</param>
        /// <param name="securityTokenReference">The token reference.</param>
        internal PartnerTokenCredentials(string securityToken, string securityTokenReference)
            : base(securityToken, true /* addTimestamp */)
        {
            EwsUtilities.ValidateParam(securityToken, "securityToken");
            EwsUtilities.ValidateParam(securityTokenReference, "securityTokenReference");

            SafeXmlDocument doc = new SafeXmlDocument();

            doc.PreserveWhitespace = true;
            doc.LoadXml(securityTokenReference);
            this.keyInfoNode = new KeyInfoNode(doc.DocumentElement);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="X509CertificateCredentials"/> class.
        /// </summary>
        /// <remarks>The X509Certificate2 argument should have private key in order to sign the message.</remarks>
        /// <param name="certificate">The X509Certificate2 object.</param>
        public X509CertificateCredentials(X509Certificate2 certificate)
            : base(null, true)
        {
            EwsUtilities.ValidateParam(certificate, "certificate");

            if (!certificate.HasPrivateKey)
            {
                throw new ServiceValidationException(Strings.CertificateHasNoPrivateKey);
            }

            this.certificate = certificate;

            string certId = WSSecurityUtilityIdSignedXml.GetUniqueId();

            this.SecurityToken = string.Format(
                X509CertificateCredentials.BinarySecurityTokenFormat,
                certId,
                Convert.ToBase64String(this.certificate.GetRawCertData()));

            SafeXmlDocument doc = new SafeXmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(string.Format(X509CertificateCredentials.KeyInfoClauseFormat, certId));
            this.keyInfoClause = new KeyInfoNode(doc.DocumentElement);
        }