Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999120
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:36:57+00:00 2026-05-20T07:36:57+00:00

The DotNetOpenAuth OAuth 2 Library requires RSAParameters to access public and private keys (example

  • 0

The DotNetOpenAuth OAuth 2 Library requires RSAParameters to access public and private keys (example in DotNetOpenAuth OAuth 2 – UriStyleMessageFormatter which uses RSAParameters to construct an RSACryptoServiceProvider).

I came across an Azure Security Whitepaper which noted that Azure installs certificates in a “certificate store with a flag indicating that the private key can be used but not exported”. Which I believe may be at the heart of this problem.

While I have been able extract the public and private keys from the cert while developing and debugging locally by referencing the certificate by it’s thumbprint (example below) I have had no luck getting the same code running in Azure.

The following code gives the error: “Key not valid for use in specified state” in Azure

    public class Global : System.Web.HttpApplication, IContainerAccessor
    {
        private static string thumbPrint = "<<my certificate thumbprint>>";
        public static readonly RSAParameters AuthorizationServerSigningPublicKey = OAuthUtil.GetPublicKey(thumbPrint);
        internal static readonly RSAParameters ResourceServerEncryptionPrivateKey = OAuthUtil.GetPrivateKey(thumbPrint); 

//....... unnecessary code omitted ..... //

    public static class OAuthUtil
        {
            public static RSAParameters GetPublicKey(string thumbPrint)
            {
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
                var cert = store.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, true)[0];
                var rsaParams = ((RSACryptoServiceProvider) cert.PublicKey.Key).ExportParameters(false);
                return rsaParams;            
            }

            public static RSAParameters GetPrivateKey(string thumbPrint)
            {
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
                var cert = store.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, true)[0];
                var rsaParams = ((RSACryptoServiceProvider) cert.PrivateKey).ExportParameters(true);
                return rsaParams;
            }
        }

Encryption / decryption code in Azure based off of the same certificate (example below) which does not require exporting the key works fine:

    public class Certificate
    {
        public string FriendlyName { get; set; }
        public string IssuedBy { get; set; }
        public string IssuedTo { get; set; }
        public string ExpirationDate { get; set; }
        public string PublicKey { get; set; }
        public string PrivateKey { get; set; }
    }

    public ActionResult Keys()
    {
        X509Certificate2Collection selectedCerts = new X509Certificate2Collection();
        var certList = new List<Certificate>();
        X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        try
        {
            store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
            foreach (X509Certificate2 cert in store.Certificates)
            {
                // Encrypt string "hello world"
                CspParameters CSPParam = new CspParameters();
                CSPParam.Flags = CspProviderFlags.UseMachineKeyStore;
                string PlainString = "hello world";
                byte[] cipherbytes = ASCIIEncoding.ASCII.GetBytes(PlainString);
                RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;
                byte[] cipher = rsa.Encrypt(cipherbytes, false);
                var encryptedString = Convert.ToBase64String(cipher);

                var cert2 = cert;
                string decryptedString = "verify = " + cert2.Verify() ;
                if (cert2.HasPrivateKey && cert2.Verify())
                {
                    // Decrypt encrypted string..
                    RSACryptoServiceProvider rsaDecrypt = (RSACryptoServiceProvider)cert2.PrivateKey;
                    byte[] cipherbytes2 = Convert.FromBase64String(encryptedString);
                    byte[] plainbytes = rsaDecrypt.Decrypt(cipherbytes2, false);
                    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                    decryptedString = enc.GetString(plainbytes);  
                }
                var certItem = new Certificate
                                   {
                                       FriendlyName = cert.FriendlyName,
                                       IssuedBy = cert.Issuer,
                                       IssuedTo = cert.SubjectName.Name,
                                       ExpirationDate = cert.NotAfter.ToString("d"),
                                       PublicKey =
                                           "Public Key: " + cert.GetPublicKeyString() + "<br/>Encrypted String: " + encryptedString + "<br/>Decrypted String: " +
                                           decryptedString,
                                       PrivateKey =
                                           "cert has private key?: " + cert.HasPrivateKey + "<br/> key algo:" +
                                           cert.GetKeyAlgorithm()
                                   };
                certList.Add(certItem);
            }
        }
        finally
        {
            store.Close();
        }
        return View(certList);
    }

Aside from rewriting the OAuth 2 library to use RSACryptoServiceProvider references instead of RSAParameters is there any way I could get this to work in Azure?

Is anyone else experiencing the same issue with DotNetOpenAuth OAuth 2 and Azure when reading certificates out of the store?

I would like to avoid hacks such as installing the certificate with export privileges using a start up task (due to security concerns).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:36:58+00:00Added an answer on May 20, 2026 at 7:36 am

    Great feedback. I’ve filed a ticket for it.Please check

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are currently delevoping an app which uses the DotNetOpenAuth library to secure a
I'm implementing an oauth provider using DotNetOpenAuth CTP library. So I have created an
I'm trying to understand how to apply the oAuth consumer library from DotNetOpenAuth in
I am struggling trying to pick apart the OAuth Service Provider example which is
I am trying to send data to DotNetOpenAuth website as described here http://msdn.microsoft.com/en-us/library/debx8sh9.aspx Sender
i have a method which takes in a DotNetOpenAuth (formally known as DotNetOpenId) Response
I trying to test an AccountController that uses DotNetOpenAuth but I am running into
Introduction We have an OpenID Provider which we created using the DotNetOpenAuth component. Everything
I am using OAuth (linq2twitter and DotNetOpenAuth) to allow a user to post comments
Using DotNetOpenAuth 3.4.3.10103 when i call: public static XDocument GetUpdates(ConsumerBase twitter, string accessToken) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.