What are Secure and Unsecure Configuration in Dynamics 365 CRM Plugin?

Secure and Unsecure ConfigurationSecure and Unsecure Configuration

In Microsoft Dynamics 365 CRM, plugins are like little helpers that extend the system’s capabilities. They can do things like saving data or updating records. To make them more flexible, there are two types of settings: Secure Configurations (like secret notes) and Unsecure Configurations (like open notes). These settings allow developers to customize how the plugins work without changing their code directly. It’s like adjusting settings to fine-tune how the plugins behave!

Understanding Secure and Unsecured Configurations

Unsecure Configuration:

  • Visibility: Accessible to all users with access to the plugin registration.
  • Storage: Stored in plain text.
  • Use Case: Suitable for non-sensitive information, such as default values or non-confidential settings.

Secure Configuration:

  • Visibility: Accessible only to users with the System Administrator or System Customizer roles.
  • Storage: Stored in an encrypted format in the database.
  • Use Case: Suitable for sensitive information like API keys, connection strings, or credentials.

Using Secure and Unsecured Configurations

Step 1: Register the Plugin with Configuration Data
  1. Open the Plugin Registration Tool.
  2. Select the Plugin Assembly you want to configure.
  3. Add a Step (or update an existing step) for your plugin.
  4. In the Step Registration window, enter your configuration data:
    • Unsecure Configuration: Enter non-sensitive data here.
    • Secure Configuration: Enter sensitive data here.
Step 2: Access Configuration Data in Plugin Code

Within your plugin code, access these configurations using the IPluginExecutionContext interface:

public class TechMasalaPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        // Obtain the execution context from the service provider.
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        // Retrieve the unsecure configuration string.
        string unsecureConfig = context.UnsecureConfig;

        // Retrieve the secure configuration string.
        string secureConfig = context.SecureConfig;

        // Use the configuration data as needed.
        if (!string.IsNullOrEmpty(unsecureConfig))
        {
            // Process the unsecure configuration data.
        }

        if (!string.IsNullOrEmpty(secureConfig))
        {
            // Process the secure configuration data.
        }
    }
}

Example Use Case: Connecting to an External Service

Let’s consider a scenario where your plugin needs to connect to an external web service. The connection requires an API key (sensitive) and a service URL (non-sensitive).

  1. Register the Plugin:
    • Use the Plugin Registration Tool.
    • In the Secure Configuration field, enter the API key.
    • In the Unsecure Configuration field, enter the service URL.

  2. Access and Use the Configuration:
    • Retrieve the configurations in your plugin code.
    • Use the secure configuration (API key) to authenticate.
    • Use the unsecured configuration (service URL) to connect to the service.
public class ExternalServicePluginTM : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        string serviceUrl = context.UnsecureConfig; // Non-sensitive info
        string apiKey = context.SecureConfig; // Sensitive info

        if (!string.IsNullOrEmpty(serviceUrl) && !string.IsNullOrEmpty(apiKey))
        {
            // Connect to the external service using the service URL and API key.
            var client = new HttpClient();
            client.BaseAddress = new Uri(serviceUrl);
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            // Perform operations with the external service.
        }
    }
}

Benefits of Using Secure and Unsecured Configurations

  1. Security: Sensitive data is stored securely and only accessible to authorized users.
  2. Flexibility: Configuration data can be changed without modifying the plugin code.
  3. Manageability: Centralized management of configuration data through the Plugin Registration Tool.

By leveraging secure and unsecured configurations, developers can create more flexible, secure, and maintainable plugins in Dynamics 365 CRM. These configurations allow for dynamic behavior adjustments while keeping sensitive data protected and easily manageable.

Leave a Reply

Your email address will not be published. Required fields are marked *