I am trying to call a Web API endpoint(https) using client certificate in Dynamics CRM plugin on Sandbox environment and I am getting the following error message.
Unhandled Exception:
System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): MyTestPlugin.PatchOnRevise: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.KeyContainerPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ActivityId>9eb9f246-31da-42d4-a8ad-a93b16c783d7</ActivityId>
<ErrorCode>-2147220956</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
<Message>Unexpected exception from plug-in (Execute): MyTestPlugin.PatchOnRevise: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.KeyContainerPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.</Message>
<Timestamp>2018-07-08T22:01:17.969528Z</Timestamp>
<ExceptionRetriable>false</ExceptionRetriable>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText>
This is the code wherein I am attaching the certificate to WebClient.
protected override WebRequest GetWebRequest(Uri _address)
{
X509Certificate2 x509Certificate2 = new X509Certificate2(_bytes);
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(_address);
request.ClientCertificates.Add(x509Certificate2);
return request;
}
I have tried to disable Sandbox, but no luck. Is there anything else I need to do to fix this issue.