This is the error I get when I execute this line of code:
IServiceManagement<IOrganizationService> OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(organizationUri);
{System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1..ctor(Uri serviceUri, Boolean checkForSecondary) at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration..ctor(Uri serviceUri, Boolean enableProxyTypes, Assembly assembly) at Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateManagement[TService](Uri serviceUri, Boolean enableProxyTypes, Assembly assembly) at Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateManagement[TService](Uri serviceUri) The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.SecurityPermission The Zone of the assembly that failed was: MyComputer}
*This post is locked for comments
Hello,
What kind of code do you use? Is it a plugin or something else? Also would be great to see full code because one line can't tell much about potential issues.
Yes it is a plugin
public static Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy GetCrmService() { string userName = "crm@test.com"; string password = "crm1234!"; string userNameDevice = "11608y5hmrajfdsg"; string passwordDevice = "TJ*a%LSsfPahg1;W"; //string domain = System.Configuration.ConfigurationManager.AppSettings["Domain"]; Uri organizationUri = new Uri("crm.api.crm.dynamics.com/.../Organization.svc"); Uri homeRealmUri = null; IServiceManagement<IOrganizationService> OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(organizationUri); AuthenticationProviderType OrgAuthType = OrganizationServiceManagement.AuthenticationType; OrganizationServiceProxy organizationProxy; if (OrgAuthType == AuthenticationProviderType.LiveId) { ClientCredentials credentials = new ClientCredentials(); credentials.UserName.UserName = userName; credentials.UserName.Password = password; ClientCredentials deviceCredentials = new ClientCredentials(); deviceCredentials.UserName.UserName = userNameDevice; deviceCredentials.UserName.Password = passwordDevice; organizationProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, deviceCredentials); } else { AuthenticationCredentials authCredentials = new AuthenticationCredentials(); authCredentials.ClientCredentials.UserName.UserName = userName; authCredentials.ClientCredentials.UserName.Password = password; AuthenticationCredentials tokenCredentials = OrganizationServiceManagement.Authenticate(authCredentials); SecurityTokenResponse responseToken = tokenCredentials.SecurityTokenResponse; organizationProxy = new OrganizationServiceProxy(OrganizationServiceManagement, responseToken); } return organizationProxy; } } }
Don't use your code for getting connection to CRM Endpoint. Use following code instead:
public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(null); //Do your operations with CRM here }
So if I execute this method, what do I pass in for the IServiceProvider?
I basically need to create a method that returns the organization proxy so that it works with the rest of my code. I do not want to re-engineer or re-write what I've done so far.
Can you suggest a way to return the value as if:
OrganizationServiceProxy organizationProxy; organizationProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, deviceCredentials); return organizationProxy;
You can't use that code because plugin has sandboxing (i.e. part of assemblies is not available and Client assembly is one of them). When you develop plugin you implement IPlugin interface in your code and IServiceProvider is passed as a parameter inside your Execute method that you have to implement in your code. Check this article that provides basics of development of plugins - msdn.microsoft.com/.../gg594416.aspx
So I have to re-write my code. ugh, thanks for pointing that out.
Ok I got it to work but I cannot write to my local hard drive.
Request for the permission of type 'System.Security.Permissions.FileIOPermission, it's happening at XDoc.Save(filename);
Just curious why do you need it but as a possible solution - move your plugin out of Isolation (register it out of sandbox). During registration you have 2 options how to register plugin - in sandbox or None. Choose none.
The plugin registration tool doesnt allow me to select None only Sandbox since its CRM Online.
I am now able to create an email in draft mode in CRM but cannot send the email because of security issues for when i try to get the organization proxy.
Can you help?
see link: community.dynamics.com/.../194211
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
HR-09070029-0 2
ED-30091530-0 1