Get X509Certificate2 : X509Certificates « Security « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Security » X509CertificatesScreenshots 
Get X509Certificate2
        

//-----------------------------------------------------------------------
// <copyright file="CertificateUtil.cs" company="MICROSOFT">
//     Copyright (c) MICROSOFT Technologies.  All rights reserved.
// </copyright>
// <summary>
// Class for handling security certificates
// </summary>
//-----------------------------------------------------------------------
namespace HCLT.Practice.WindowsAzure.Solution.STSAuthentication
{
    using System;
    using System.Security.Cryptography.X509Certificates;

    /// <summary>
    /// A utility class which helps to retrieve an x509 certificate
    /// </summary>
    public class CertificateUtil
    {
        /// <summary>
        /// Method is used to retrieve certificates
        /// </summary>
        /// <param name="name">certificate name</param>
        /// <param name="location">certificate location</param>
        /// <param name="subjectName">certificate subject name</param>
        /// <returns>valid certificate</returns>
        public static X509Certificate2 GetCertificate(StoreName name, StoreLocation location, string subjectName)
        {
            X509Store store = new X509Store(name, location);
            X509Certificate2Collection certificates = null;
            store.Open(OpenFlags.ReadOnly);

            try
            {
                X509Certificate2 result = null;

                // Every time we call store.Certificates property, a new collection will be returned.
                certificates = store.Certificates;

                for (int i = 0; i < certificates.Count; i++)
                {
                    X509Certificate2 cert = certificates[i];

                    if (cert.SubjectName.Name.ToLower() == subjectName.ToLower())
                    {
                        if (result != null)
                        {
                            throw new ApplicationException(string.Format("There are multiple certificates for subject Name {0}", subjectName));
                        }

                        result = new X509Certificate2(cert);
                    }
                }

                if (result == null)
                {
                    throw new ApplicationException(string.Format("No certificate was found for subject Name {0}", subjectName));
                }

                return result;
            }
            finally
            {
                if (certificates != null)
                {
                    for (int i = 0; i < certificates.Count; i++)
                    {
                        X509Certificate2 cert = certificates[i];
                        cert.Reset();
                    }
                }

                store.Close();
            }
        }
    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.ClientCertificates
2.Gets a certificate from a given store.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.