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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Security issues in my code C#

(0) ShareShare
ReportReport
Posted on by
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

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Security issues in my code C#

    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.

  • Community Member Profile Picture
    on at
    RE: Security issues in my code C#

    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;
    
            }
    
        }
    }
    


  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Security issues in my code C#

    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
            }
    


  • Community Member Profile Picture
    on at
    RE: Security issues in my code C#

    So if I execute this method, what do I pass in for the IServiceProvider?

  • Community Member Profile Picture
    on at
    RE: Security issues in my code C#

    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;

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Security issues in my code C#

    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

  • Community Member Profile Picture
    on at
    RE: Security issues in my code C#

    So I have to re-write my code. ugh, thanks for pointing that out.

  • Community Member Profile Picture
    on at
    RE: Security issues in my code C#

    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);

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Security issues in my code C#

    Hello,

    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.

  • Community Member Profile Picture
    on at
    RE: Security issues in my code C#

    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.

Helpful resources

Quick Links

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans