Key not valid for use in specified state.

425 views
Skip to first unread message

Anthony Ponceot

unread,
Jan 23, 2015, 4:46:26 PM1/23/15
to google-api-d...@googlegroups.com
Hi,

I have an application that is connecting to Google API and is working fine from my development computer.

I've made a Console Application to try to debug my problem.

public void Connect() {
if (certificate != null)
{
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
User = Configuration.googleApps.impersonationUser,
Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser ,
DirectoryService.Scope.AdminDirectoryGroup
}
}.FromCertificate(certificate));

.....
}

}

When I run this application on another machine (Windows Server 2012), I have the following exception when the ServiceAccountCredential instanciate :

Key not valid for use in specified state.


Any idea ?
Thanks in advance.


See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.

at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils.ExportCspBlob(SafeKeyHandle hKey, Int32 blobType, ObjectHandleOnStack retBlob)
at System.Security.Cryptography.Utils.ExportCspBlobHelper(Boolean includePrivateParameters, CspParameters parameters, SafeKeyHandle safeKeyHandle)
at System.Security.Cryptography.RSACryptoServiceProvider.ExportCspBlob(Boolean includePrivateParameters)
at Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer.FromCertificate(X509Certificate2 certificate) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Auth.DotNet4\OAuth2\ServiceAccountCredential.cs:line 80
at GoogleAppSync.GoogleConnector.Connect() in d:\Mes Documents\InternalTools\MinervaDirectorySync\GoogleAppsSync\GAppsSync.cs:line 30

Darrell Sveistrup

unread,
Jun 17, 2015, 4:29:00 PM6/17/15
to google-api-d...@googlegroups.com
I guess you have some thing like this before that code:
var certificate = new X509Certificate2(certKeyFilePath, passCode, X509KeyStorageFlags.Exportable );


Add in X509KeyStorageFlags.MachineKeySet | 509KeyStorageFlags.PersistKeySet
So you have like this:
var certificate = new X509Certificate2(certKeyFilePath, passCode, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet );
Reply all
Reply to author
Forward
0 new messages