Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > API Authentication >

XeroAPI.NET Security Exception on Rackspace Cloud Sites

Started by Radomir Perzina -   in API Authentication

We are using XeroAPI.NET for private application access to Xero via v2.0 api, however we are having difficulties with running it on Rackspace Cloud Sites using .NET 4.0. I followed provided examples and here is the piece of code which crashes:
IOAuthSession consumerSession = new XeroApi.OAuth.XeroApiPrivateSession(
_xeroUserAgentString,
_xeroConsumerKey,
new X509Certificate2(_xeroPublicKeyData2, _xeroPublicKeyPassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet)
);

specifically calling constructor new XeroApi.OAuth.XeroApiPrivateSession(…)
throws exception: “System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.KeyContainerPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.”

When I call it locally on my computer it all works fine, but on Rackspace Cloud Sites environment with .NET 4.0 it throws the above not too much saying exception.
Interestingly when I switch .NET version on Rackspace Cloud Sites to .NET 3.5 it works fine even on Rackspace, but when using .NET 4.0 it throws the exception above.

I am sending also complete dump of the exception with stack trace:
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.KeyContainerPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

SecurityException: Request for the permission of type 'System.Security.Permissions.KeyContainerPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +31
System.Security.CodeAccessPermission.Demand() +46
System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +160
System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) +75
System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() +89
System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize) +162
System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey() +194
XeroApi.OAuth.XeroApiPrivateSession.CreateConsumerContext(String userAgent, String consumerKey, X509Certificate2 signingCertificate) +179
XeroApi.OAuth.XeroApiPrivateSession..ctor(String userAgent, String consumerKey, X509Certificate2 signingCertificate) +46
CSLCommon.XeroCommon.SendXmlToXeroVersion2(XmlDocument xmlDoc, String type) +134

Any help how to get it working on Rackspace Cloud Sites Environment with .NET 4.0 would be much appreciated.
+1 - having the same issues here. Will let you know if we reach any resolution using .NET 4.0, yet to try switching to 3.5
 

Dynamics CRM  

Dear Sir,

You need to check the permission of the specific folder where you certificates are saved.

thanks
 

Raveen Rasaiyah