X509SecurityTokenProvider.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / Selectors / X509SecurityTokenProvider.cs / 1305376 / X509SecurityTokenProvider.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.IdentityModel.Selectors 
{
    using System.IdentityModel.Tokens; 
    using System.Security.Cryptography.X509Certificates; 

    public class X509SecurityTokenProvider : SecurityTokenProvider, IDisposable 
    {
        X509Certificate2 certificate;

        public X509SecurityTokenProvider(X509Certificate2 certificate) 
        {
            if (certificate == null) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("certificate");
            } 

            this.certificate = new X509Certificate2(certificate);
        }
 
        public X509SecurityTokenProvider(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findValue)
        { 
            if (findValue == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("findValue"); 
            }

            X509CertificateStore store = new X509CertificateStore(storeName, storeLocation);
            X509Certificate2Collection certificates = null; 
            try
            { 
                store.Open(OpenFlags.ReadOnly); 
                certificates = store.Find(findType, findValue, false);
                if (certificates.Count < 1) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.CannotFindCert, storeName, storeLocation, findType, findValue)));
                }
                if (certificates.Count > 1) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.FoundMultipleCerts, storeName, storeLocation, findType, findValue))); 
                } 

                this.certificate = new X509Certificate2(certificates[0]); 
            }
            finally
            {
                SecurityUtils.ResetAllCertificates(certificates); 
                store.Close();
            } 
        } 

        public X509Certificate2 Certificate 
        {
            get { return this.certificate; }
        }
 
        protected override SecurityToken GetTokenCore(TimeSpan timeout)
        { 
            return new X509SecurityToken(this.certificate); 
        }
 
        public void Dispose()
        {
            this.certificate.Reset();
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK