web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Online 2015 Plugin gets another CRM org service error: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

(0) ShareShare
ReportReport
Posted on by

Hi, Guys.

I am building a plugin which is triggered by a creation of contact. Then the plugin will build connection and get Another CRM organization service, to create a new contact as well in that CRM instance. Both CRM instances are 2015 on-premise. And I get error when creating the second CRM proxy. 

The error is : System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

 code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;

using System.ServiceModel.Description;
using System.Security;
using Microsoft.Crm.Sdk.Messages;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;


namespace SyncronizePlugin
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class Class1:IPlugin
{
string newfirstname, newlastname;

static IOrganizationService service1;
private static bool AcceptAllCertificatePolicy(Object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}

public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

Entity entity = (Entity)context.InputParameters["Target"];
string createdentityname = entity.LogicalName;

Guid createdid = entity.Id;
ClientCredentials credentials = new ClientCredentials();
//credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
credentials.UserName.UserName = @"XYX\admin";
credentials.UserName.Password = "Pass12345";
Uri OrganizationUri = new Uri("xyz.xyz.com/.../Organization.svc");
Uri HomeRealmUri = null;
OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null); //thrown error in  this line
service1 = (IOrganizationService)serviceProxy;



ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;

ColumnSet col = new ColumnSet(true);
Entity createdobj = service.Retrieve(createdentityname, createdid, col);

if (createdobj.Contains("firstname"))
{
newfirstname = createdobj.Attributes["firstname"].ToString();
}
if (createdobj.Contains("lastname"))
{
newlastname = createdobj.Attributes["lastname"].ToString();
}

Entity Contact = new Entity();
Contact.LogicalName = "contact";
Contact.Attributes.Add("firstname", newfirstname);
Contact.Attributes.Add("lastname", newlastname);

service1.Create(Contact);

}

}
}

*This post is locked for comments

I have the same question (0)
  • David Jennaway Profile Picture
    14,065 on at

    It looks like you've registered the plugin assembly in the sandbox. Plugins in the sandbox are only permitted to make anonymous web service calls. If you need to connect to another Crm organisation, then your plugin assembly will need to be registered outside the sandbox

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans