Azure Storage connection string How to implement to applicate - azure

I need to create access to the Azure storage from application. Application requires storage, where to put file. This code below, I am trying to put where application requires path to store file. Am I doing right? How can I implement this into XML of Application, so then it starts to write files to cloud storage.
<connectionStrings>
<add name= "DefaultConnection" connectionString="DefaultEndpointsProtocol=https;AccountName=;AccountKey=;/>
</connectionStrings>

Please take a look at this doc for example.
In your app.config, please add:
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
</appSettings>
And in your code, please use:
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
Then you can take use of azure storage.

if you login to the Blob image storage on Azure. it should populate the connection string by default. PFB Reference

Related

How to access Azure Key Vault Using SSIS Script Task?

Need to access Azure Key Vault using SSIS Script Task in C#, to assign the Azure SQL Connection String to Connection Manager in SSIS.
To Achieve the above requirements , You may refer the below workaround.
Create an application ASP.NET MVC application.
Create Azure keyvault secret for the database connection string. And give permission to your app from Key Vault->Access Policy.
In application level use the Connected Service to access the Key Vault-> Secret for connection string , Which added in the code in webconfig and added nuget packages then test in your local and then deploy to Azure.
For example:
web.config
<configSections><section name="configBuilders"
type="System.Configuration.ConfigurationBuildersSection,
System.Configuration, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b0xxxxxxx3a"
restartOnExternalChanges="false"
requirePermission="false" />
<builders>
<add name="AzureKeyVault" vaultName="Prod-ConSt-01"type" Microsoft
.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder,
Microsoft.Configuration.ConfigurationBuilders.Azure,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=xxxxxxxx"
vaultUri="https://mywebsite-prod-const-01.vault.azure.net/" /><
/builders>
</configBuilders>
<connectionStrings configBuilders="AzureKeyVault">
<add name="ProductionConnstr" connectionString="from key vault"
providerName="System.Data.SqlClient" />
</connectionStrings>
For more information please refer the below links:-
MICROSFT DOCUMENTATION: Use a managed identity to connect Key Vault to an Azure web app in .NET
SO THREAD: Not able to get Azure Key-Vault working in app when deployed to Azure & Azure SDK with SSIS

Publishing Web.Config to Azure removes Azure storage connection string

I've got a web.config that contains my SQL connection string and my Azure Blob storage connection string.
A Web.Config transformation replaces my Local SQL connection string with the Azure one.
When I publish the site to Azure, the Blob storage connection string is deleted and replaced with a duplicate SQL connection string, but with the blob storage string's name.
The only way I've found to fix is to log in via FTP and manually change the erroneous Storage connection string with the correct one from my local machine.
How do I get VS to publish my web config to Azure and leave it alone!!!
Web.Config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="StorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=;AccountKey=" />
</connectionStrings>
Web.Release.Config
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=.database.windows.net,1433;Database=;User ID=#;Password=!;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
I had a similar issue to yours. I'm not sure why but when you define the connection strings in the "Configure tab" in the azure portal and associate a "Linked Resource" on the linked resource tab it may override certain properties in the Web.config transform causing unexpected results. One of the options when you set up a new azure website is linking to (or creating a new) database to associate with your website - thereby automatically assigning the related connection string which may try to override your transform operation defined in the Web.Release.config.
Check and see if removing all connection strings and linked resources inside the "Azure Portal" fixes your problem. Just make sure that you have both your production database and storage connections strings defined properly in the Web.Release.config.
I struggled with this problem this morning and I came up with a solution for VS2015/17.
So I have an Azure VM, and to publish my web app on this machine, I used the Web deploy to an Azure VM proposed by VS.
I put my connection strings in an external file, so the useful part of my web.config looks like this :
</entityFramework>
<connectionStrings configSource="ConnectionStrings.config">
</connectionStrings>
</configuration>
in order to prevent VS of adding some connection strings during publication (ADO.Net code first MSSQL database connection string in my case), you can edit the following file in your project :
...\MyProject\Properties\PublishProfiles\YourPublishProfile - WebDeploy.pubxml
In this file look into the ItemGroup part and edit it to delete the connection strings you don't need:
<PublishDatabaseSettings>
<Objects xmlns="">
<ObjectGroup Name="MyProject.Models.MSSQL_DB" Order="1" Enabled="False">
<Destination Path="" />
<Object Type="DbCodeFirst">
<Source Path="DBContext" DbContext="MyProject.Models.MSSQL_DB, MyProject" Origin="Convention" />
</Object>
</ObjectGroup>
</Objects>
</PublishDatabaseSettings>
</PropertyGroup>
<ItemGroup>
<here are some entries delete the ones you don't need/>
</ItemGroup>
Be careful, if you add a file in this repertory, there is chances that it breaks the publication process on VS. Don't add file, just edit.

How to connect Azure blob storage through website MVC4

I develop a website on Windows Azure, MVC4. I try to upload and get photos from blobs.
My related code (that I got from other examples on web):
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
var blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("abc");
container.CreateIfNotExists();
var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
My related web.config code:
<add name="StorageConnection" connectionString="DefaultEndPointsProtocol=https;AccountName=abc;AccountKey=*******"/>
When I run I got a format exception. Please note that it is web site.
As per some posts, when I put my connection string to app.config, no solution.
Thank you very much for any assistance.
You shall not use connectionStrings section to store Azure Storage Connection string!
Use appSettings instead and you will have no issues. It is confusion that the Storage credentials is also named "Connection string", but it has nothing to do with the connectionStrings section in your configuration file. connectionStrings section is intended solely for use DataBase connection strings.

Azure CloudDrive HTTPS Uri

From this MSDN article it says that the connection string for connecting to the storage account must be HTTP and not HTTPS.
When I use this constructor:
public CloudDrive (
Uri uri,
StorageCredentials credentials
)
Does that mean the Uri to the page blob must also be HTTP and not HTTPS? I am a bit confused regarding which parameter (or both together) fits the 'connection string' description.
This scenario does not seem to be easily testable in dev emulator.
The URI (think of as the server-portion of the connection string) to the page blob represents the namespace + container + blob of your storage account. The credentials represent the user/pass which together with the URI comprise the connection string to the Azure cloud storage service.
The URI will always be HTTP assuming you are using the local emulator.
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
When deploying to Azure, the URI scheme will be whatever you assign it in the service configuration (ServiceDefinition.csdef / ServiceConfiguration.Cloud.cscfg).
CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("CloudDrive.DataConnectionString");
You would just want to assign CloudDrive.DataConnectionString to have DefaultEndpointsProtocol=http (the default if omitted, but you could be explicit).
ServiceDefinition.csdef
<ServiceDefinition>
<WebRole>
<!-- .... -->
<ConfigurationSettings>
<Setting name="CloudDrive.DataConnectionString" />
</ConfigurationSettings>
</WebRole>
</ServiceDefinition>
ServiceConfiguration.Cloud.cscfg
<ServiceConfiguration>
<Role>
<ConfigurationSettings>
<Setting name="CloudDrive.DataConnectionString" value="DefaultEndpointsProtocol=http;AccountName=YOURNAMESPACE;AccountKey=YOURKEY" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>

Set the application ConnectionString in the Service Configuration instead of web.config in Azure

I have an application currently in Azure and whenever we push it into the Staging segment, we cannot truly test since the connection string is pointing to the prod database.
Someone mentioned to me that you should be able to set the connection string in the ServiceConfiguration.cscfg file instead (or with) the web.config file. That way you can change the connection string in the Azure portal instead of republishing a who app.
Does anyone know how to do this?
In your ServiceConfiguration.cscfg file add:
<ServiceConfiguration ... />
<Role ... />
<ConfigurationSettings>
<Setting name="DatabaseConnectionString" value="put your connection string here" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
Now you have a connection string you can change by editing the configuration inside the azure portal.
Then anytime you need to retrieve the connection string you can do it using:
using Microsoft.WindowsAzure.ServiceRuntime;
...
String connString = RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString")
You may need to add Microsoft.WindowsAzure.ServiceRuntime.dll to your references.
RoleEnviroment.IsAvailable can be used to test if your are running in Azure, and if not to fall back to your web.config settings.
using System.Configuration;
using Microsoft.WindowsAzure.ServiceRuntime;
...
if (RoleEnvironment.IsAvailable)
{
return RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString");
}
else
{
return ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
}
This article has a more verbose explanation of the above.
For Entity Framework, you do not need to provide a providerName, it's already inside in the connectionstring. The reason why it does not work when it's in azure settings is, it contains &quot symbol which needs to be transalated back to " before creating a new EntityConnection. You can do it using HttpUtility.HtmlDecode in System.Web.
Basically you need to define these setting in Azure service configuration file. Check here. Once defined these can be changed from Azure portal.

Resources