I have build a command line app which connects to CRM and outputs some data to an FTP location. It is working well.
I thought I would deploy it to Azure as a WebJob so that it could be scheduled.
I received errors from the deviceidmanager relating to the file location of the deviceid details. I found an article and made the changes below which seemed to fix the issue.
private static class LiveIdConstants { public const string RegistrationEndpointUriFormat = @"https://{0}/ppsecure/DeviceAddCredential.srf"; public static readonly string FileNameFormat = Path.Combine( Environment.GetEnvironmentVariable("WEBROOT_PATH"),"{0}{1}.xml"); public const string ValidDeviceNameCharacters = "0123456789abcdefghijklmnopqrstuvqxyz"; //Consists of the list of characters specified in the documentation public const string ValidDevicePasswordCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^*()-_=+;,./?`~"; } private string Encrypt(string value) { if (string.IsNullOrEmpty(value)) { return value; } byte[] encryptedBytes = ProtectedData.Protect(Encoding.UTF8.GetBytes(value), null, DataProtectionScope.LocalMachine); return Convert.ToBase64String(encryptedBytes); } private string Decrypt(string value) { if (string.IsNullOrEmpty(value)) { return value; } byte[] decryptedBytes = ProtectedData.Unprotect(Convert.FromBase64String(value), null, DataProtectionScope.LocalMachine); if (null == decryptedBytes || 0 == decryptedBytes.Length) { return null; } return Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length); }
Note the use of LocalMachine the WebRoot Path.
Things were fine for 24 hours and then I started getting the following error:
System.InvalidOperationException: Unable to Deserialize XML (Operation = Loading Device Credentials from Disk):
System.InvalidOperationException: There is an error in XML document (5, 5). ---> System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.
at System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope)
[09/02/2016 14:38:00 > 86fffa: INFO] at Microsoft.Crm.Services.Utility.DeviceUserName.Decrypt(String value) in C:\Projects-Online\***\LetterBatchTool\Helpers\deviceidmanager.cs:line 981
I am unsure why the code worked and then broke without intervention. It had been running every hour for testing purposes.
Could the file have become corrupt? Do I need the file? Is there maybe a way to bypass it or store the data another way? Perhaps a simple amendment to make it use hardcoded values or create the data fresh every time?