Hi,
I think I found what the problem is... You're attaching your certificate to the request after you send the HTTP request... Please move this call:
request.GetRequestStream()
after where you call:
request.ClientCertificates.Add(certificate);
Other problems in your code:
1. bodyText is invalid, missing bold part:
"<ChangeConfiguration xmlns=\"http://schemas.microsoft.com/windowsazure\""
2. Attached certificate twice.
3. The code to post should be:
Stream dataStream = request.GetRequestStream();
dataStream.Write(buf, 0, buf.Length);
dataStream.Close();
The modified version based on your code:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
string thumbprint = "[thumbprint]";
void Run()
{
var subID="[subscription id]";
var sname = "[service name]";
var sdname = "[Production or Staging]";
var config = File.ReadAllText("myconfig file path");
changeConfiguration(subID, sname, config, sdname);
}
public void changeConfiguration(string subscriptionId,string serviceName, string config, string deploymentName)
{
byte[] encodedConfigbyte = new byte[config.Length];
encodedConfigbyte = System.Text.Encoding.UTF8.GetBytes(config);
string encodedConfig = Convert.ToBase64String(encodedConfigbyte);
Uri changeConfigRequestUri = new Uri("https://management.core.windows.net/" + subscriptionId + "/services/hostedservices/" + serviceName
+ "/deploymentslots/" + deploymentName + "/?comp=config");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(changeConfigRequestUri);
request.Headers.Add("x-ms-version", "2010-10-28");
request.Method = "POST";
string bodyText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<ChangeConfiguration xmlns=\"http://schemas.microsoft.com/windowsazure\""
+ ">"
+ "</ChangeConfiguration>";
byte[] buf = Encoding.UTF8.GetBytes(bodyText);
request.ContentType = "application/xml";
request.ContentLength = buf.Length;
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
certStore.Open(OpenFlags.ReadOnly);
}
catch (Exception e)
{
if (e is CryptographicException)
{
Console.WriteLine("Error: The store is unreadable.");
}
else if (e is SecurityException)
{
Console.WriteLine("Error: You don't have the required permission.");
}
else if (e is ArgumentException)
{
Console.WriteLine("Error: Invalid values in the store.");
}
else
{
throw;
}
}
X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
certStore.Close();
if (certCollection.Count == 0)
{
throw new Exception("Error: No certificate found containing thumbprint ");
}
X509Certificate2 certificate = certCollection[0];
request.ClientCertificates.Add(certificate);
Stream dataStream = request.GetRequestStream();
dataStream.Write(buf, 0, buf.Length);
dataStream.Close();
try
{
WebResponse response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
// code here
}
}
}
Allen Chen [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Allen Chen [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
