【问题标题】:Create a plugin to decrypt custom field of Case Entity while Retrieve in MS Dynamics CRM Online创建插件以在 MS Dynamics CRM Online 中检索时解密案例实体的自定义字段
【发布时间】:2016-04-20 14:08:24
【问题描述】:

我需要在 MS Dynamics CRM 在线门户中查看案例时加密自定义字段并自动解密。 我创建了两个插件,一个用于在 PreCaseCreate 进行加密,另一个用于在 PostCaseRetrieve 进行解密。加密插件工作正常,但解密插件不起作用(表示加密内容在在线门户中查看时未解密)。 下面是解密代码

// <copyright file="PostCaseRetrieve.cs" company="">
// Copyright (c) 2016 All Rights Reserved
// </copyright>
// <author></author>
// <date>4/20/2016 1:58:24 AM</date>
// <summary>Implements the PostCaseRetrieve Plugin.</summary>
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
// </auto-generated>

namespace CRMCaseEntityDecryptPlugin.Plugins
{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using System.Text;
using System.Security.Cryptography;
using Microsoft.Xrm.Sdk.Query;

/// <summary>
/// PostCaseRetrieve Plugin.
/// </summary>    
public class PostCaseRetrieve : Plugin
{
    /// <summary>
    /// Initializes a new instance of the <see cref="PostCaseRetrieve"/> class.
    /// </summary>
    public PostCaseRetrieve()
        : base(typeof(PostCaseRetrieve))
    {
        base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Retrieve", "incident", new Action<LocalPluginContext>(ExecutePostCaseRetrieve)));

        // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
        // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
    }

    /// <summary>
    /// Executes the plug-in.
    /// </summary>
    /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
    /// <see cref="IPluginExecutionContext"/>,
    /// <see cref="IOrganizationService"/>
    /// and <see cref="ITracingService"/>
    /// </param>
    /// <remarks>
    /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
    /// The plug-in's Execute method should be written to be stateless as the constructor
    /// is not called for every invocation of the plug-in. Also, multiple system threads
    /// could execute the plug-in at the same time. All per invocation state information
    /// is stored in the context. This means that you should not use global variables in plug-ins.
    /// </remarks>
    protected void ExecutePostCaseRetrieve(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        // TODO: Implement your custom Plug-in business logic.
        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;
        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parmameters.
            Entity entity = (Entity)context.InputParameters["Target"];
            if (entity.LogicalName.ToLower().Equals("incident"))
            {
                try
                {
                    ColumnSet cols = new ColumnSet(new String[] { "title", "description", "new_phicontent" });
                    var incident = service.Retrieve("incident", entity.Id, cols);
                    if (incident.Attributes.Contains("new_phicontent"))
                    {
                        string PHIContent = incident.Attributes["new_phicontent"].ToString();
                        byte[] bInput = Convert.FromBase64String(PHIContent);

                        UTF8Encoding UTF8 = new UTF8Encoding();
                        //Encrypt/Decrypt strings which in turn uses 3DES (Triple Data Encryption standard) algorithm
                        TripleDESCryptoServiceProvider tripledescryptoserviceprovider = new TripleDESCryptoServiceProvider();

                        //Alow to compute a hash value for Encryption/Decryption
                        MD5CryptoServiceProvider md5cryptoserviceprovider = new MD5CryptoServiceProvider();

                        tripledescryptoserviceprovider.Key = md5cryptoserviceprovider.ComputeHash(ASCIIEncoding.ASCII.GetBytes("secretkey"));
                        tripledescryptoserviceprovider.Mode = CipherMode.ECB;
                        ICryptoTransform icryptotransform = tripledescryptoserviceprovider.CreateDecryptor();

                        string DecryptedText = UTF8.GetString(icryptotransform.TransformFinalBlock(bInput, 0, bInput.Length));
                        incident["new_phicontent"] = DecryptedText;
                        service.Update(incident);
                    }

                }
                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }
            }
        }
    }

}

}

我也尝试了 PreCaseRetrieve 事件,但我没有得到结果

请提供一些解决方案来解决此问题。 提前致谢

【问题讨论】:

  • 把加密和解密的职责放在一个单独的类中,并为其编写一些单元测试。您很快就会发现问题所在。仅使用插件类进行事件处理。

标签: asp.net c#-4.0 dynamics-crm-2011


【解决方案1】:

将您的插件保留为帖子插件。

来自InputParametersTarget 对象是发送给客户端的对象,因此如果修改目标对象,则修改发送给客户端的内容。所以不要检索incident,然后更新incident。相反,如果entity 包含 new_phicontent 属性,那么您知道客户端请求了该属性并且需要对其进行解密,因此请解密该值,然后更新entity["new_phicontent"]。这是更新的代码:

// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName.ToLower().Equals("incident"))
{
    try
    {
        if (entity.Attributes.Contains("new_phicontent"))
        {
            string PHIContent = entity.Attributes["new_phicontent"];
            byte[] bInput = Convert.FromBase64String(PHIContent);

            // removed for brevity

            string decryptedText = UTF8.GetString(icryptotransform.TransformFinalBlock(bInput, 0, bInput.Length));
            entity["new_phicontent"] = decryptedText;
        }
    }
    catch (FaultException ex)
    {
        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
    }
}

【讨论】:

  • 我注销了PreCaseRetrieve插件,修改了上面提到的PostCaseRetrieve并部署了,但是内容解密没有发生
  • 它可能只是一些小错误。在 Visual Studio 中调试您的插件:blogs.msdn.microsoft.com/devkeydet/2015/02/17/…
猜你喜欢
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多