0
votes

I have a console application that can delete and create jobs in a Job collection in Azure Scheduler. I have created a local certificate to create a SchedulerClient and have deployed this certificate to via the Azure Portal -> Settings -> Management Certificates -> Upload.

I have put the above code into a Scheduled Job within a Mobile Services App.

First the job failed as it states it can not find the certificate :-

Message='Attempting to create scheduled job 'Job Name' failed with the following message: 'A Certificate with Thumbprint 'xxxx' could not be located.'.', Exception=System.ArgumentException: A Certificate with Thumbprint 'xxxx' could not be located. at TechCare.Mobile.Services.JobManager.GetStoreCertificate(String thumbprint) at TechCare.Mobile.Services.JobManager..ctor() at lambda_method(Closure , Object[] ) at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate(), Id=6a06732a-1dba-4ce5-affe-dee1596877de, Category='App.Controllers.Jobs'

So then I created a Base64String version of the certificate to which then this error was now returning

Exception=Hyak.Common.CloudException: ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription. at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) at Microsoft.WindowsAzure.Scheduler.JobOperationsExtensions.List(IJobOperations operations, JobListParameters parameters) at TechCare.Mobile.Services.JobManager.ClearJobCollection() at techcaremobilewebservicesService.ScheduledJobs.dailyRoutines.CreateNotificationSchedule() at techcaremobilewebservicesService.ScheduledJobs.dailyRoutines.ExecuteAsync(), Id=00000000-0000-0000-0000-000000000000

So then I found a method to download a published profile from the Azure Portal and pulled out the string which makes up the management certificate from the file and tried that but that failed as well.

Message='Attempting to create scheduled job 'Job Name' failed with the following message: 'The system cannot find the file specified. '.', Exception=System.Security.Cryptography.CryptographicException: The system cannot find the file specified.

at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData) at TechCare.Mobile.Services.JobManager.GetStoreCertificate(String thumbprint), Id=31dd37b3-53cb-4f8b-8873-6155f25913dd, Category='App.Controllers.Jobs'

To which I then found some flags that were suggested to add on when creating the certificate

X509KeyStorageFlags flags = X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable;

return new X509Certificate2(Convert.FromBase64String(certRaw), string.Empty, flags);

But then this failed as well.

Message='Attempting to create scheduled job 'Job Name' failed with the following message: 'Access denied. '.', Exception=System.Security.Cryptography.CryptographicException: Access denied.

at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) at TechCare.Mobile.Services.JobManager.GetStoreCertificate(String thumbprint), Id=07a6329b-f1ec-44b6-8446-12bcf616d336, Category='App.Controllers.Jobs'

I would appreciate some help to fix this issue as I've spent more time trying to fix this than how long the actual development took :-S

1

1 Answers

0
votes

The problem is that you need to authorize your Mobile Service to be able to manage your Scheduler Service. This requires a set of certificates because it would not be secure to do these management operations from user code.

See this document for more information: Authenticating Service Management Requests

Brady Gaster has a great step-by-step blog post on how to do this for Azure Websites, the procedure is similar for Mobile Services: Managing Web Sites from Web Sites using the Azure Management Libraries for .NET