When trying to deploy a web application to Azure using a service account with the Google .net client library it is returning with the following error
502 - Web server received an invalid response while acting as a gateway or proxy server.
Code example:
var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail) {
Scopes = new string[] { AnalyticsService.Scope.Analytics };
}.FromCertificate(certificate));
// Create the service.
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",
});
The code above works when run in development. However when deployed to AZURE it returns an error.
It has taken me about 2 hours to track down the problem
The issue is to do with the with the way that Azure deals with certificates.
By changeing the following line
var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
to this
var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
The web application and the service account now work on Azure. I hope this helps someone in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With