Reputation: 1993
I have a system configuration entity where I store credentials of a Web Service. I need to encrypt the password field (new_SharePointServicePassword) and I want my plugin to do the encryption.
So, I have created a new assembly with the following events registered.
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "new_systemconfiguration", new Action<LocalPluginContext>(ExecutePostSystemConfigurationCreate)));
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_systemconfiguration", new Action<LocalPluginContext>(ExecutePostSystemConfigurationCreate)));
My plugin code is as below.
string plainTextPassword = SystemConfiguration.new_SharePointServicePassword;
string encryptedPassword;
this.TracingService.Trace("Generating Keys");
Encryption encryption = new Encryption(localContext.PluginExecutionContext.OrganizationId.ToString());
this.TracingService.Trace("Generating Keys - Completed");
if (!string.IsNullOrEmpty(plainTextPassword))
{
this.TracingService.Trace("Encrypting password");
encryptedPassword = encryption.Encrypt(plainTextPassword);
this.TracingService.Trace("Encrypting password - Completed");
SystemConfiguration.new_SharePointServicePassword = encryptedPassword;
localContext.OrganizationService.Update(SystemConfiguration);
}
The steps registered against the Plugin are for Post Operation for both Create and Update messages. The Update message is filtered on the new_SharePointServicePassword attribute.
Questions
localContext.OrganizationService.Update(SystemConfiguration);
required? Is it because, my plugin executes after the changes have been committed to the database?Upvotes: 0
Views: 1070
Reputation: 4575
I would recommend you to run the plugin on Pre Operation
. Because when you enter the password through UI, it will save the unencrypted password first. User will have to refresh to see the encryption. Also if you have auditing enabled on password field. It will show the unencrypted password in Audit log. On Pre Operation
you don't need following line:
localContext.OrganizationService.Update(SystemConfiguration);
Make sure that you replace 40
with 20
in your code.
Upvotes: 2