Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Give Access to Certificate in Store

Give Access to Certificate in Store

Scheduled Pinned Locked Moved C#
comcryptographyhelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikker_123
    wrote on last edited by
    #1

    I just want to add X509Certificate to store, and enable user to read it's private key. I snatched part of code from here but it won't work. private static void PlaceInStore(X509Certificate2 cert) { X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); try { store.Open(OpenFlags.ReadWrite); using (RSACryptoServiceProvider csp = cert.PrivateKey as RSACryptoServiceProvider) { CspKeyContainerInfo kci = csp.CspKeyContainerInfo; CryptoKeySecurity cks = kci.CryptoKeySecurity; cks.SetAccessRule(new CryptoKeyAccessRule("ARCHITECT\\testuser", CryptoKeyRights.FullControl, AccessControlType.Allow)); } if (!store.Certificates.Contains(cert)) store.Add(cert); } finally { store.Close(); } } Help anyone?

    B 1 Reply Last reply
    0
    • M mikker_123

      I just want to add X509Certificate to store, and enable user to read it's private key. I snatched part of code from here but it won't work. private static void PlaceInStore(X509Certificate2 cert) { X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); try { store.Open(OpenFlags.ReadWrite); using (RSACryptoServiceProvider csp = cert.PrivateKey as RSACryptoServiceProvider) { CspKeyContainerInfo kci = csp.CspKeyContainerInfo; CryptoKeySecurity cks = kci.CryptoKeySecurity; cks.SetAccessRule(new CryptoKeyAccessRule("ARCHITECT\\testuser", CryptoKeyRights.FullControl, AccessControlType.Allow)); } if (!store.Certificates.Contains(cert)) store.Add(cert); } finally { store.Close(); } } Help anyone?

      B Offline
      B Offline
      blackjack2150
      wrote on last edited by
      #2

      I once had to do a similar thing - adding a certificate to a particular certificate store. And I discovered something very strange: Nothing happened if I used the Add method, but if instead I used the AddRange method it worked. AddRange takes as parameter a collection of certificates. Just create a new collection, add your certificate to it and call AddRange. And never lose your hope. :)

      M 1 Reply Last reply
      0
      • B blackjack2150

        I once had to do a similar thing - adding a certificate to a particular certificate store. And I discovered something very strange: Nothing happened if I used the Add method, but if instead I used the AddRange method it worked. AddRange takes as parameter a collection of certificates. Just create a new collection, add your certificate to it and call AddRange. And never lose your hope. :)

        M Offline
        M Offline
        mikker_123
        wrote on last edited by
        #3

        Dunno... Add method works great for me. Anyways I found some kind of solution googling, it works, but I don't like it. Anyways, if someone has better solution please post, until then I'll use this:

            private static void PlaceInStore(X509Certificate2 cert)
            {
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        
                try
                {
                    store.Open(OpenFlags.ReadWrite);
        
                    if (!store.Certificates.Contains(cert))
                        store.Add(cert);
        
                    int indexInStore = store.Certificates.IndexOf(cert);
                    cert = store.Certificates\[indexInStore\];
        
                    AddAccessToCertificate(cert, "ARCHITECT\\\\testuser");
                }
                finally
                {
                    store.Close();
                }
            }
        
            private static void AddAccessToCertificate(X509Certificate2 cert, string user)
            {
                RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;
        
                if (rsa != null)
                {
                    string keyfilepath =
                        FindKeyLocation(rsa.CspKeyContainerInfo.UniqueKeyContainerName);
        
                    FileInfo file = new FileInfo(keyfilepath + "\\\\" +
                        rsa.CspKeyContainerInfo.UniqueKeyContainerName);
        
                    FileSecurity fs = file.GetAccessControl();
        
                    NTAccount account = new NTAccount(user);
                    fs.AddAccessRule(new FileSystemAccessRule(account,
                    FileSystemRights.FullControl, AccessControlType.Allow));
        
                    file.SetAccessControl(fs);
                }
            }
        
            private static string FindKeyLocation(string keyFileName)
            {
                string text1 =
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                string text2 = text1 + @"\\Microsoft\\Crypto\\RSA\\MachineKeys";
                string\[\] textArray1 = Directory.GetFiles(text2, keyFileName);
                if (textArray1.Length > 0)
                {
                    return text2;
                }
                string text3 =
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                string text4 = text3 + @"\\Microsoft\\Cry
        
        D S 2 Replies Last reply
        0
        • M mikker_123

          Dunno... Add method works great for me. Anyways I found some kind of solution googling, it works, but I don't like it. Anyways, if someone has better solution please post, until then I'll use this:

              private static void PlaceInStore(X509Certificate2 cert)
              {
                  X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
          
                  try
                  {
                      store.Open(OpenFlags.ReadWrite);
          
                      if (!store.Certificates.Contains(cert))
                          store.Add(cert);
          
                      int indexInStore = store.Certificates.IndexOf(cert);
                      cert = store.Certificates\[indexInStore\];
          
                      AddAccessToCertificate(cert, "ARCHITECT\\\\testuser");
                  }
                  finally
                  {
                      store.Close();
                  }
              }
          
              private static void AddAccessToCertificate(X509Certificate2 cert, string user)
              {
                  RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;
          
                  if (rsa != null)
                  {
                      string keyfilepath =
                          FindKeyLocation(rsa.CspKeyContainerInfo.UniqueKeyContainerName);
          
                      FileInfo file = new FileInfo(keyfilepath + "\\\\" +
                          rsa.CspKeyContainerInfo.UniqueKeyContainerName);
          
                      FileSecurity fs = file.GetAccessControl();
          
                      NTAccount account = new NTAccount(user);
                      fs.AddAccessRule(new FileSystemAccessRule(account,
                      FileSystemRights.FullControl, AccessControlType.Allow));
          
                      file.SetAccessControl(fs);
                  }
              }
          
              private static string FindKeyLocation(string keyFileName)
              {
                  string text1 =
                  Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                  string text2 = text1 + @"\\Microsoft\\Crypto\\RSA\\MachineKeys";
                  string\[\] textArray1 = Directory.GetFiles(text2, keyFileName);
                  if (textArray1.Length > 0)
                  {
                      return text2;
                  }
                  string text3 =
                  Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                  string text4 = text3 + @"\\Microsoft\\Cry
          
          D Offline
          D Offline
          DotNet_Naeem
          wrote on last edited by
          #4

          Hi Mikker, I know its been a longgggggggggggggggggggggg time since you have replied to this post but I bumped into your post and I wanna do exactly what you have mentioned...but the problem I am facing is that I am confused as to how to call the PlaceInStore() function ? I tried using HTTPHandlers in WCF service but it didnt work so I am kinda stuck now. Pleas help ?? Anybody??? :) Naeem

          1 Reply Last reply
          0
          • M mikker_123

            Dunno... Add method works great for me. Anyways I found some kind of solution googling, it works, but I don't like it. Anyways, if someone has better solution please post, until then I'll use this:

                private static void PlaceInStore(X509Certificate2 cert)
                {
                    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            
                    try
                    {
                        store.Open(OpenFlags.ReadWrite);
            
                        if (!store.Certificates.Contains(cert))
                            store.Add(cert);
            
                        int indexInStore = store.Certificates.IndexOf(cert);
                        cert = store.Certificates\[indexInStore\];
            
                        AddAccessToCertificate(cert, "ARCHITECT\\\\testuser");
                    }
                    finally
                    {
                        store.Close();
                    }
                }
            
                private static void AddAccessToCertificate(X509Certificate2 cert, string user)
                {
                    RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;
            
                    if (rsa != null)
                    {
                        string keyfilepath =
                            FindKeyLocation(rsa.CspKeyContainerInfo.UniqueKeyContainerName);
            
                        FileInfo file = new FileInfo(keyfilepath + "\\\\" +
                            rsa.CspKeyContainerInfo.UniqueKeyContainerName);
            
                        FileSecurity fs = file.GetAccessControl();
            
                        NTAccount account = new NTAccount(user);
                        fs.AddAccessRule(new FileSystemAccessRule(account,
                        FileSystemRights.FullControl, AccessControlType.Allow));
            
                        file.SetAccessControl(fs);
                    }
                }
            
                private static string FindKeyLocation(string keyFileName)
                {
                    string text1 =
                    Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                    string text2 = text1 + @"\\Microsoft\\Crypto\\RSA\\MachineKeys";
                    string\[\] textArray1 = Directory.GetFiles(text2, keyFileName);
                    if (textArray1.Length > 0)
                    {
                        return text2;
                    }
                    string text3 =
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                    string text4 = text3 + @"\\Microsoft\\Cry
            
            S Offline
            S Offline
            SHUBHAM SHARMA 0
            wrote on last edited by
            #5

            How can we work if we have to access the remote server certificates. For remote server it is showing an exception keyset not found Thanks in Advance. Please enlighten me

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups