Reputation: 3678
I am testing whether my Azure SAS token is working or not in the production environment. I have created C# code to test the same.
class Program
{
static void Main(string[] args)
{
var storageAccount = new StorageAccount();
//Configure Storage Account and get the reference of a newly created table
try
{
var cloudTableReference = storageAccount.CloudTableInstance;
if (cloudTableReference != null)
{
Console.WriteLine("Table {0} created successfully using SAS token", cloudTableReference.Name);
}
else
{
Console.WriteLine("There is an error in new table creation");
}
}
catch (Exception ex)
{
Console.WriteLine("Error is: - {0}", ex.Message);
}
}
}
And below is the StorageAccount.cs
class StorageAccount
{
private CloudStorageAccount CloudStorageAccount { get; set; }
private CloudTableClient CloudTableClient { get; set; }
private CloudTable CloudTable { get; set; }
/// <summary>
/// Fetches Storage Account connection string from App.Config and returns a CloudTableClient object.
/// </summary>
/// <returns>CloudTableClient</returns>
public CloudTableClient ConfigureStorageAccount()
{
// Retrieve the storage account from the connection string.
if (ConfigurationManager.AppSettings["StorageConnectionString"] != string.Empty)
{
CloudStorageAccount = CloudStorageAccount.Parse(
ConfigurationManager.AppSettings["StorageConnectionString"]);
try
{
CloudTableClient = CloudStorageAccount.CreateCloudTableClient();
return CloudTableClient;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
return null;
}
}
var storageCred = new StorageCredentials(ConfigurationManager.AppSettings["SASToken"]);
try
{
CloudTableClient = new CloudTableClient(
new StorageUri(new Uri(ConfigurationManager.AppSettings["StorageAccountUri"])), storageCred);
return CloudTableClient;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
return null;
}
}
/// <summary>
/// Returns a CloudTable instance for a Table
/// </summary>
public CloudTable CloudTableInstance
{
get
{
try
{
ConfigureStorageAccount();
CloudTable = CloudTableClient.GetTableReference("ProdTestTable");
CloudTable.Create();
return CloudTable;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="SASToken" value="SAS_TOKEN_VALUE" />
<add key="StorageAccountUri" value="URI"/>
</appSettings>
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy proxyaddress="http://XXX.XX.X.XXX"/>
</defaultProxy>
</system.net>
</configuration>
I run this code on an App server which doesn't have internet access so I have specified the proxy in the app.config
file. It runs smoothly till this point, but the request generated from App server reaches proxy as an HTTP one instead of HTTPS. My SAS token spr parameter is https
. Can anyone point out to the possible error?
Upvotes: 0
Views: 393
Reputation: 8499
but the request generated from App server reaches proxy as an HTTP one instead of HTTPS.
I can't reproduce the issue on my side. I used Fiddler as my proxy.
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy proxyaddress="http://127.0.0.1:8888"/>
</defaultProxy>
</system.net>
After run your code, I can see that the request URL in fiddler is start with https.
if (ConfigurationManager.AppSettings["StorageConnectionString"] != string.Empty)
I am confused by this code. It will be null and throw an exception later if I don't set StorageConnectionString value in app setting. I need to configure it as empty string in app setting.
<add key="StorageConnectionString" value=""/>
Please check this configuration in your app.config, if you set a connection string with HTTP protocol instead of SASToken.
In addition, you also could disable the HTTP request temporarily from your Storage server by changing secure transfer required property on Azure portal. If the creating table operation also works fine. It will prove that the transfer protocol is HTTPS.
Upvotes: 1