Share via


Creating the security helper application

Open the file that contains the method named Main and complete the following steps:

  1. Add namespace references.

    To simplify the use of several classes and methods, add using statements for the following namespaces:

    • System.Configuration

    • Microsoft.Dynamics.Security

  2. Add data members.

    Add private data members to your class. To use service context information, add a SecurityContext object and an ApplicationKey object. Add an OperationKey, TaskKey, or RoleKey for each operation, task, or role you are adding to the security service.

    The following code example shows the objects used to add security for the sample Leads service.

    private SecurityContext securityContext;
    private ApplicationKey appKey;
    private OperationKey getByKeyOpKey;
    private OperationKey getListOpKey;
    private OperationKey deleteOpKey;
    private OperationKey createOpKey;
    private OperationKey updateOpKey;
    private TaskKey viewTaskKey;
    private TaskKey manageTaskKey;
    private RoleKey roleKey;
    private static string tenant;
    
  3. Initialize the data members.

    To populate the security context and object keys, add a private method named InitializeComponent to the class.

    private void InitializeComponent()
    {
    
    }
    

    Use the InitializeComponent method to instantiate a security context object. Set the ID property of the application key object to the GUID that identifies the Web Services for Microsoft Dynamics GP application. To identify Web Service for Microsoft Dynamics GP, always use the following GUID:

    25cc1a21-2cc4-4b13-a1c8-eea186fb688a
    

    The following code sample shows how to populate the ApplicationKey property of the security context. Notice how the application key ID is populated with the GUID that specifies the Web Services for Microsoft Dynamics GP application. Also note that if the tenant was supplied as a command line parameter, it is passed as a value for the security context.

    securityContext = new SecurityContext();
    appKey = new ApplicationKey();
    appKey.Id = "25cc1a21-2cc4-4b13-a1c8-eea186fb688a";
    securityContext.ApplicationKey = appKey;
    securityContext.TenantName = tenant;
    

    Next, create an operation key for each security operation. To populate the ID of each operation key, you must follow the required naming convention. Each security key ID must contain the GUID that identifies your document class followed by the operation type. The GUID is the same GUID you used in the GuidAttribute of your document type class. For more information about setting the GuidAttribute, see Defining the document type.

    The following table shows the required format for each type of operation.

    Operation

    Operation key ID

    GetByKey

    <GUID>GetByKey

    GetList

    <GUID>GetList

    Create

    <GUID>Create

    Delete

    <GUID>Delete

    Update

    <GUID>Update

    Warning: The service requires the operation key IDs to contain both the GUID and operation type. If you do not include both, you will not be able to access your data.

    The following code example shows how to create the security operation keys for Leads. Notice how each ID contains the following GUID value:

    2852BB26-3BA8-4663-9613-033327D7F3C2
    

    This is the same GUID that was used in the GuidAttribute of the Lead class. To view the GuidAttribute of the Lead class, see Defining the document type.

    getByKeyOpKey = new OperationKey();
    getByKeyOpKey.Id = "2852BB26-3BA8-4663-9613-033327D7F3C2GetByKey";
    
    getListOpKey = new OperationKey();
    getListOpKey.Id = "2852BB26-3BA8-4663-9613-033327D7F3C2GetList";
    
    deleteOpKey = new OperationKey();
    deleteOpKey.Id = "2852BB26-3BA8-4663-9613-033327D7F3C2Delete";
    
    createOpKey = new OperationKey();
    createOpKey.Id = "2852BB26-3BA8-4663-9613-033327D7F3C2Create";
    
    updateOpKey = new OperationKey();
    updateOpKey.Id = "2852BB26-3BA8-4663-9613-033327D7F3C2Update";
    

    Next, create a task key for each task. Populate the ID property of each task with a GUID that uniquely identifies the task.

    The following code example creates two task keys for leads. The GUID values were generated using the Create GUID tool from Visual Studio. Notice how the braces were removed from each GUID.

    viewTaskKey = new TaskKey();
    viewTaskKey.Id = "A33871C6-8393-4040-803D-FA1EF7306136";
    
    manageTaskKey = new TaskKey();
    manageTaskKey.Id = "7029D575-7FB4-4194-A905-4B0276C5D890";
    

    Finally, create a role key. Populate the ID property with a GUID that specifies a Dynamics Security Service role. To specify the Dynamics Security Service Superuser role use the following GUID:

    e18b321a-9548-48fb-b75a-dee0a618ddaa
    

    Hint: To retrieve the ID of other roles, use the GetRoles method of the Dynamics Security Service. For more information about how to retrieve role information, see the Microsoft Dynamics Security Service Reference.

    The following code example creates a role key. The GUID value specifies the Superuser role in the Dynamics Security Service.

    roleKey = new RoleKey();
    roleKey.Id = "e18b321a-9548-48fb-b75a-dee0a618ddaa";
    
  4. Add a constructor.

    Create a constructor method for the class. Use the constructor to run the initialization method.

    The following code example shows the constructor. Notice how the constructor calls the initialization method that was created earlier.

    public Program()
    {
        InitializeComponent();
    }
    
  5. Implement command line parameters.

    In the Main method, add support for command line parameters. Use the parameter "/load" to add security metadata. Use the parameter "/remove" to delete security metadata. An optional second parameter is used to specify the tenant in a multitenant environment. If you are not using tenants, this parameter can be omitted. The following code example shows how to implement the command line parameters.

    static void Main(string[] args)
    {
        // Get the tenant name, if supplied.
        if (args.Length == 2)
        {
            tenant = args[1].ToString();
        }
        else
        {
            tenant = "";
        }
    
        if ((args[0].ToLowerInvariant() == "/load") ||
        (args[0].ToLowerInvariant() == "/remove"))
        {
    
        }
        else
        {
            Console.WriteLine("InstallLeadSecurityMetadata.exe has an
                incorrect set of parameters");
            Console.WriteLine("Valid parameters are: /load or /remove");
            Console.WriteLine();
        }
    
        Console.WriteLine("Press any key to quit.");
        Console.ReadKey();
    }
    
  6. Create an instance of the class.

    Add a statement to the Main method that instantiates the class.

    Program addLeads = new Program();
    
  7. Specify the app.config information.

    In your app.config file, add a a reference to the WSInstallAppSettings.config file. The file reference provides configuration settings for your application that enable your helper application to update the Dynamics Security Service. The WSInstallAppSetting.confgi file is typically found in the following folder:

    c:\Program Files\Microsoft Dynamics\GPWebServices\ServiceConfigs

    The app.config file also contains entries that are used to locate the Tenant Discover Service if you are installing in a multitenant environment. It also contains entries for diagnostic logging that can be activated if you need to debug the application.

    The following code example shows the configuration settings for the security loader application. Notice how the filepath specifies the expected location of the WSInstallAppSettings configuration file, the endpoints for the Tenant Discovery Service, and the diagnostic logging settings.

    <?xml version="1.0"?>
    <configuration>
        <appSettings file="ServiceConfigs\WSInstallAppSettings.config"/>
        <system.serviceModel>
            <bindings configSource="ServiceConfigs\WSBindings.config"/>
            <client>
                <endpoint name="DiscoveryServiceHttp" binding="wsHttpBinding" bindingConfiguration="DiscoveryServiceBindingHttp" contract="Microsoft.Dynamics.MultitenantServices.ServicesInterface.ITenantDiscoveryService" address=""/>
                <endpoint name="DiscoveryServiceHttps" binding="wsHttpBinding" bindingConfiguration="DiscoveryServiceBindingHttps" contract="Microsoft.Dynamics.MultitenantServices.ServicesInterface.ITenantDiscoveryService" address=""/>
            </client>
        </system.serviceModel>
    
    <system.diagnostics>
        <switches>
            <add name="ApplicationTraceSwitch" value="0"/>
        </switches>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="dynamicsListener"/>
            </listeners>
        </trace>
        <sharedListeners>
            <add name="dynamicsListener"
                type="System.Diagnostics.TextWriterTraceListener"
                initializeData="C:\Program Files\Microsoft Dynamics\GPWebServices\Logs\Tracing\LeadSecurityMetadata.log"/>
        </sharedListeners>
    </system.diagnostics>
    </configuration>