2016-04-20 9 views
1

Ich habe eine Anforderung, die benutzerdefinierte Feld zu verschlüsseln und automatisch entschlüsseln beim Anzeigen des Falles in MS Dynamics CRM Online Portal. Ich habe zwei Plugins ist für Encrypt bei PreCaseCreate und der andere ist bei PostCaseRetrieve .Der Plugin zu entschlüsseln für die Verschlüsselung funktioniert prima, aber Plugin für Entschlüsselung funktioniert nicht (was bedeutet, dass der verschlüsselte Inhalt entschlüsselt nicht während des Betrachtens im Online-Portal). Unten finden Sie den Code zur EntschlüsselungErstellen Sie ein Plugin zum Entschlüsseln benutzerdefinierte Feld von Case Entity beim Abrufen in MS Dynamics CRM Online

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

} 

}

ich mit PreCaseRetrieve Ereignis auch versucht, aber ich habe nicht Ergebnis

bitte eine Lösung Geben Sie bekam dieses Problem zu beheben. Vielen Dank im Voraus

+0

Legen Sie die Verantwortung für die Verschlüsselung und Entschlüsselung in einer separaten Klasse und schreiben Sie einige Komponententests dafür. Sie werden bald herausfinden, was falsch ist. Verwenden Sie die Plug-in-Klasse nur für die Ereignisbehandlung. –

Antwort

0

Lassen Sie Ihr Plugin als Post-Plugin.

Target Objekt von ist das Objekt, das an den Client gesendet wird. Wenn Sie also das Zielobjekt ändern, ändern Sie, was an den Client gesendet wird. Rufen Sie also nicht incident ab und aktualisieren Sie dann incident. Wenn entity das Attribut new_phicontent enthält, wissen Sie, dass der Client das Attribut angefordert hat und entschlüsselt werden muss. Entschlüsseln Sie den Wert, und aktualisieren Sie dann entity["new_phicontent"]. Hier ist der aktualisierte Code:

// 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); 
    } 
} 
+0

Ich habe das Plugin ** PreCaseRetrieve ** nicht registriert und die ** PostCaseRetrieve ** wie oben erwähnt und implementiert, aber die Inhaltsentschlüsselung ist nicht passiert –

+0

Es ist wahrscheinlich nur ein kleiner Fehler. Debuggen Sie Ihr Plug-in in Visual Studio: https://blogs.msdn.microsoft.com/devkeydet/2015/02/17/debug-crm-online-plugins/ – Polshgiant