How to add Gitlab custom variable in app.config xml file - c#

I'm working on C# project using Gitlab CI and would like to hide secrets in app.config file.
I added one variable in Gitlab and in app.config like this:
<add key="User" value="username" />
<add key="Password" value=$Password />
Then, when i try to build application in gitlab CI using msbuild, i receive an error:
$ msbuild "$PROJECTNAME.sln" /consoleloggerparameters:ErrorsOnly /t:Test_project /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet
app.config(5,33): error MSB3249: Application Configuration file "app.config" is invalid. '$' is an unexpected token. The expected token is '"' or '''. Line 7, position 33. [/builds/project/Test_project/Test_project.Service/Test_project.Service.csproj]
Please advice, how to add Gitlab environment variable in app.config xml file.

For .xml files it should work using ${env.MY_VAR_DEFINED_IN_GITLAB}
Where MY_VAR_DEFINED_IN_GITLAB, as specified, is the variable defined in Gitlab Project (or group) -> Settings -> CI/CD -> Variables
Therefore in your case should work:
<add key="User" value=${env.GITLAB_VAR_USERNAME}/>
<add key="Password" value=${env.GITLAB_VAR_PASSWORD}/>

Related

Azure Pipeline Cant Access Azure Artefact Nuget Project Feed Even With Correct Permissions And Nuget Authenticate Task

I have a really strange issue where I have a project based nuget feed which one pipeline publishes to, which works fine, then another pipeline which needs to restore a project which uses this nuget feed.
The problem is I have followed all the instructions on this such as:
Make sure Build Service has permissions
Make sure NuGetAuthenticate 0 or 1 is called
Ensure there is a nuget.config with the feed included
So for example the nuget.config looks like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="azure-feed" value="http://pkgs.dev.azure.com/<org>/<project>/_packaging/<feed-name>/nuget/v3/index.json" />
</packageSources>
</configuration>
Then the pipeline section looks like:
steps:
- task: NuGetAuthenticate#0
- task: DotNetCoreCLI#2
displayName: dotnet restore
inputs:
command: restore
nugetConfigPath: 'nuget.config'
feedsToUse: config
This all works fine in the IDE (VS and Rider) and the pipelines that publish and read the nuget package are all in the same azure devops project as the feed.
When the build runs I see the authenticate step run:
Setting up the credential provider to use the identity '<project> Build Service (<org>)' for feeds in your organization/collection starting with:
https://pkgs.dev.azure.com/<org>/
https://<org>.pkgs.visualstudio.com/
Which is all correct and is pointing to the correct feeds, but when the restore runs the error below occurs:
error NU1301: Unable to load the service index for source http://pkgs.dev.azure.com/<org>/<project>/_packaging/<feed-name>/nuget/v3/index.json.
All the articles online say to try switching to NuGetAuthenticate#0 or enabling higher level settings to allow build service project scopes to not be constrained, as well as confirming all permissions are correct, none of that has solved the problem.
The issue here is that the feed in the nuget.config file is using http not https and for some reason the nuget authenticate task will ONLY authenticate the https url not the unsecure http one which the IDEs work fine with.
So changing:
<add key="azure-feed" value="http://pkgs.dev.azure.com/<org>/<project>/_packaging/<feed-name>/nuget/v3/index.json" />
to:
<add key="azure-feed" value="https://pkgs.dev.azure.com/<org>/<project>/_packaging/<feed-name>/nuget/v3/index.json" />
Fixes it all and it works, this was never mentioned as a possible solution online so I thought it prudent to put here for any future generations who have the same issue.

secrets.xml cannot be found at the auto-generated path

I'm trying to use a local secrets.xml file but an error tells me that it can't be found.
I right-clicked on the project and selected 'Manage User Secrets, which created the GUID-named folder and file for me. That same GUID appears in the userSecretsId attribute as below...
<configBuilders>
<builders>
<add name="Secrets" optional="false" userSecretsId="[a GUID]" type="..." />
</builders>
</configBuilders>
But at runtime an exception shows that the file cannot be found.
I then switched to using userSecretsFile and pasted a copy of the file to C:\tmp\secrets.xml. This worked.
So I then pasted the path of the auto-generated secrets.xml - i.e....
%APPDATA%\Microsoft\UserSecrets\<userSecretsId>\secrets.xml
...into the userSecretsFile attribute...
<add name="Secrets" optional="false" userSecretsFile="C:\Users\[my username]\AppData\Roaming\Microsoft\UserSecrets\946bd95c-42a4-45da-aad0-a7874c97fd64\secrets.xml" type="..." />
...and still an exception is thrown saying that the file can't be found.
I'm running Visual Studio 2019 with administrator privileges. Why does it appear to be prevented from accessing the file?

VSTS/TFS set environment variable ASP.NET core

I'm trying to deploy an ASP.NET Core application to IIS using VSTS with the following tasks
However, after much googling and browsing through MS docs I couldn't find a way to set environment variables for the deployment. The variables I set in the release definition in environment scope aren't getting set as environment variables.
Any idea how to achieve that?
The environment variables you set in VSTS are just used for the deployment itself (ie anything that VSTS is doing such as building your application or running unit tests), but the runtime application will use whichever ones are on the server hosting it.
You will need to set the environment variables on the IIS server that VSTS is deploying to if you want your deployed application to use them as well. Microsoft docs show how to set this depending on your server: Setting the environment
Update in response to comments:
The reccommended way to set environment variables is on the machine itself - ie. log in to the IIS server you are deploying to and add the ASPNETCORE_ENVIRONMENT environment variable there in system properties -> advanced settings -> environment variables
If for some reason you aren't able to do this, you can set them in the Web.config file (according to that documentation). If you are always setting the same value you should be able to just put what you need in the Web.config like so
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
If you really need the XML transforms (which, honestly, I'm not sure you do in this situation - this is for altering the Web.config file at deployment time based on the build configuration. As somebody else mentioned, with asp.net core the reccommended config setup is appsettings[.environment].json files which are automagically loaded based on the matching machine level ASPNETCORE_ENVIRONMENT env variable), you need to actually define the transformations in a transform file using the correct syntax and have it replace the parts you want to change. This is obviously the more difficult option.
See: How to: Transform Web.config When Deploying a Web Application Project for creating the transformation files and Web.config Transformation Syntax for Web Project Deployment Using Visual Studio for the configuration syntax if you choose to go down that path
Something like this (unable to currently test but this should give you an idea - note the transform namespace on the transform file and the xdt: attributes). I believe the transform file that gets loaded matches the build configuration which you may need to configure as part of the VSTS task:
Web.config
<configuration>
<system.webServer>
<aspNetCore ...>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
Web.Release.config (transform file for build configuration "Release")
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<aspNetCore ...>
<environmentVariables>
<environmentVariable xdt:Transform="Replace" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Production" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
For ASP.NET Core 1.x projects with a web.config you can use the below.
Since your deployment has a "Dev" environment, commit to your project the following config file:
web.Dev.config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<aspNetCore>
<environmentVariables xdt:Transform="InsertIfMissing" />
<environmentVariables>
<environmentVariable xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" />
<environmentVariable xdt:Transform="Replace" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
The above will create the environmentVariables section in your web.config if it does not already exist.
Replace "Dev" in web.Dev.config with other environment names as necessary.
ASPNETCORE_ENVIRONMENT used as an example above, change for other
variables.
Remove the xmlns="" attribute from the configuration element above if your web.config does not have the same namespace attribute on the configuration element.
In your project.json, add under publishOptions => include:
"web.dev.config"
In VSTS deployment make sure to check "XML transformation" under the IIS Web App Deploy task:
Here is the powershell script I use within Release pipeline (I don't like setting ASPNETCORE_ENVIRONMENT within the build)
arguments:
-p $(System.DefaultWorkingDirectory)\$(Build.DefinitionName)\drop\testbld-Test\web.config -e Development
Inline Script:
param ([string]$p,[string]$e)
$doc = new-object System.Xml.XmlDocument
$location = "$p"
$doc.Load($location)
$subNode = $doc.CreateElement("environmentVariable")
$node = $doc.CreateElement("environmentVariables")
$doc.SelectSingleNode("//aspNetCore").AppendChild($node)
$doc.SelectSingleNode("//environmentVariables").AppendChild($subNode)
foreach($nd in $subNode) {$nd.SetAttribute("name", "ASPNETCORE_ENVIRONMENT");$nd.SetAttribute("value", "$e");}
$doc.Save($location)
I add it as an argument during the "Publish" step of the build:
/p:EnvironmentName=Development
Then it will be added to the web.config of the build output.
Refer to these steps below:
Set config files properties (e.g. web.config, web.QA.config), Copy to Output Directory: Copy if newer)
.NET Core task (Command: restore)
.NET Core task (command: build)
.NET Core task (Command: publish; Check Publish Web Projects option; Arguments: --configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory); Check Zip Published Projects option)
Publish Build Artifacts (Path to publish:$(build.artifactstagingdirectory))
Open release definition, change environment name (e.g. QA, match the config file name)
IIS Web Deploy task: (Package or Folder: $(System.DefaultWorkingDirectory)\**\*.zip; Check XML transformation option (it is based on Environment name to look for transform source file)
Then the web.[environmentname].config file (e.g. web.QA.config) will be transformed to web.config file.
You also can do it through XDT transform task, (the files can’t be in the zip file, so un-check Zip Published Projects option: step4, and archive files through Archive Files task in release)
Another approach to setting environment variables (other than using the XML transform approach) is to add a Powershell task which uses appCmd command to set environment variables in the ApplicationPool scope
C:\Windows\system32\inetsrv\appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='XyzPool'].environmentVariables.[name='ASPNETCORE_ENVIRONMENT',value='Dev']" /commit:apphost

Visual studio (whole thing) 2010 fails to upgrade older solutions?

Visual C++ 2010 constantly fails with upgrading Visual C++ 6.0 Workspaces to the new format. It keeps having problems with the same project file, which opens perfectly in Visual C++ 6.0 (Which I'd really like to refrain from using.) I also tried importing only the project file with no avail, and then even just moving the headers and code into a new project, which obviously doesn't work. (the project was the SoundFX 2000 source code, by Software System Consultants)
I also attempted to open an older version of a C# Workspace, and that failed to. Its always the same error: "error", no more, no less...
I'm running this on an old, XP machine (for backwards compatibility testing).
On my 7 machine with Visual C# 2015, it had no issue converting Visual C# 2008 code. Does anyone know what is going on? why can't I convert it? how can I get a more detailed output than just "error"
In order to get detailed info of the error, add the following section in the configuration section and after configSections section in the devenv.exe.config file located in Microsoft Visual Studio 10.0\Common7\IDE subfolder of the VS installation folder:
<system.diagnostics>
<switches>
<add name="CPS" value="4" />
</switches>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="c:\VS2010Debug.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
This will create the file c:\VS2010Debug.log (you can change the name and location obviously) after VS is restarted.
Next you must create a cmd file (e.g. VS2010.cmd) with the following contents:
cd /d "%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Common7\ide\"
start devenv.exe /Log
exit
Again you must edit the above path according to your own VS2010 installation location.
This will log VS activities in a log file located here (by default):
%APPDATA%\Microsoft\VisualStudio\Version\ActivityLog.xml
Start VS 2010 by executing the cmd file, convert your project and examine the log file for additional info about the conversion errors.

CloudConfigurationManager.GetSetting returning null

Following instructions here I have:
var connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
But connectionString is null, here is my app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="StorageConnectionString"
connectionString="DefaultEndpointsProtocol=https;AccountName=storage;AccountKey=key" />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Had the same problem. Instead of using a connection string, use the configuration->appSettings->add key like this...
<configuration>
<appSettings>
<add key="StorageConnectionString" value="[ConnectionStringHere]" />
</appSettings>
</configuration>
As per documentation in MSDN http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.cloudconfigurationmanager.aspx
Only configuration settings within the appSettings tag can be read by CloudConfigurationManager. If your configuration settings are within a different tag, calling GetSetting will return Null.
Well this works, even if the comment doesn't fit, because I do have a ref to CloudConfigManager:
If you are creating an application with no reference to Microsoft.WindowsAzure.CloudConfigurationManager, and your connection string is located in the web.config or app.config as show above, then you can use ConfigurationManager to retrieve the connection string. You will need to add a reference to System.Configuration.dll to your project and add another namespace declaration for it:
using System.Configuration;
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
I had the same problem. I had updated the project to use Azure SDK 2.0. I updated NuGet packages for my web and worker roles, but the Azure project in Visual Studio was still on the old version.
To fix this, Right-Click on your Azure project and select Properties. Under the Application tab, you'll see a button to Update your Azure SDK.
Make sure all your references are in synch. There's the 2012-06 library and 2012-10 Set them to Copy Local = true and verify SDK version. I dealt with the exact same thing, drove me nuts.
This happened to me when I upgraded the Azure SDK to version 2.2.
To fix it I changed the packages.config to use a newer version of the Azure ConfigurationManager.
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.1.0" targetFramework="net45" />
Based on my understanding, I'd like to point out that CloudConfigurationManager.GetSetting will look into web.config if you're running out of a cloud service. It will look into cscfg if you're inside a cloud service.
Please refer this
link.
Following this tutorial:
You can get configuration settings like this:
RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")
I got this after upgrading Azure SDK from 2.0 to 2.2. I was able to fix by:
Right-Clicking Azure project and selecting Properties. Update Azure SDK as per the Application tab. (Thanks to rattrick's answer).
Right click to Manage NuGet Packages. On the left click Updates and update WindowsAzure.ConfigurationManager.
I had the same problem (two times).
Even after restarting Visual Studio and after restarting the Azure emulator the CloudConfigurationManager.GetSetting("SettingName") returns null.
I was sure that it has worked before and I had the latest SDK.
So the solutions was restarting my PC and after that CloudConfigurationManager.GetSetting("SettingName") returns the right value.
I got the same issue this am after revisiting my Azure solution (Web + Worker role) to update it for Azure 2.5. Reviewing the help for CloudConfigurationManager.GetSetting, if its running under a cloud platform (Azure) it reads from the ServiceConfiguration.csfg, if running as a .net web app, reads from app or web.config.
So my fix was to simply change the start up project back to the Azure cloud project, not the Web project.
I was getting null because it was hosted in the wrong platform and reading from the .config files with no settings.
(Doh!)
It is an old thread but I wanted to share my solution if issue is not resolved by above mentioned methods then make sure that Azure Storage Emulator is running when you run the application; at least for me this happened. For me I had to create a class to handle emulator issue as mentioned here...
http://blog.simontimms.com/2013/08/28/configuration-settings-in-an-azure-worker-role/
class ConfigurationProvider
{
private static string GetStorageConnectionString(string name)
{
try
{
return RoleEnvironment.GetConfigurationSettingValue(name);
}
catch (SEHException)
{
return System.Configuration.ConfigurationManager.ConnectionStrings[name].ConnectionString;
}
}
public static string StorageConnectionString()
{
return GetStorageConnectionString("StorageConnectionString");
}
public static string DefaultConnection()
{
return GetStorageConnectionString("DefaultConnection");
}
}
I had quite similar problems. I updated from Azure SDK 2.0 to 2.2 - during this process I used the NuGet Manager to update the Microsoft.WindowsAzure.Storage to the latest. The PackageManager automatically took Microsoft.WindowsAzure.Configuration to 1.8.0.0. I was not able to get this running (it was for .Net 2.0!?). After I manually set all References to
Microsoft.WindowsAzure.Storage 2.1.0.0
Microsoft.WindowsAzure.Configuration 2.0.0.0
everything worked.
I think this is because of the way CloudConfigurationManager.GetSetting loads the assembly and calls the funktions (via reflection).
Same here after upgrading Azure SDK from 2.2 to 2.3.:
Right-Click the Azure project select Properties. In the Application tab click "Upgrade..." (Thanks to rattrick's answer).
Then there was one more error to resolve:
Trying to run the Azure Project in the Compute Emulator threw an exception:
System.Configuration.ConfigurationErrorsException was unhandled
Message: An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in Microsoft.WindowsAzure.ServiceRuntime.dll
Additional information: konnte nicht erstellt werden.
In the "Error List" Window of VS2013 there was the following Warning:
Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets 1635
I let VS resolve this warning and everything worked fine.
This worked for me...
using System.Configuration;
...
var connectionString = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;
I had the same problem. None of the advices worked for me, but the "issue" was straightforward. One simple has to understand how this class works.
It does not go into your app.config / web.config or wherever you store your application settings. CloudConfigurationManager works with ServiceConfiguration.*.cscfg and ServiceConfiguration.csdef. The .csdef must contain a definition of the setting, but not its value under the ConfigurationSettings section. The settings themselves sit in .cscfg files (under the same section but including the value; I suppose the reason for the double definition is to make sure both the cloud and the local configurations have the same settings).
You can set these either by right-clicking your role in Visual Studio and selecting Properties -> Settings (in case of StorageConnectionString, simply pick "Your subscription", if your storage account is connected to the cloud service), or by editing them manually. If you mess up the settings, you'll get an exclamation mark icon.
Simple as that.
Was getting a null value with when passing a literal string as well after installing Azure SDK 2.6 (was working before).
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureStorage"].ConnectionString);
Replaced the literal string and it worked fine.
string connectionStr = "AzureStorage";
var connectionstring = ConfigurationManager.ConnectionStrings[connectionStr].ConnectionString;
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);

Categories

Resources