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

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?

Related

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

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}/>

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.

change web.config back to previous setting

I have split my App_Code folder into VB and CS folders, to accommodate the two class files at compile time. I added the following to the web.config file to make it work.
<compilation debug="true" strict="false" explicit="true" defaultLanguage="C#">
<codeSubDirectories>
<clear/>
<add directoryName="VBCode" />
<add directoryName="CSCode" />
</codeSubDirectories>
</compilation>
I now want to remove the two folders and go back to a single vb only... IF I delete the above code from the web.config file I get the following error:
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: The code subdirectory '/mysite/App_Code/VBCode/' does not exist.
Source Error:
An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Source File: E:\web\web.config Line: 75
This is on a hosted site and it appears I don't have access to the machine.config or ability to view it?
How can I go back to previous configuration?
EDIT:
I understand the use of <clear/> but I'm looking to delete the folders from the site... refference: https://msdn.microsoft.com/en-us/library/ms228265(VS.85).aspx

Exception of type 'System.Web.HttpUnhandledException' was thrown. while modifying web.config file

I want to change web.config file appsetings values which is located in the server. i have given full permission to that folder still unable to access and edit the web.config file
while executing the code its giving error saying - Exception of type 'System.Web.HttpUnhandledException' .unable to access file resides in server
error:
System.UnauthorizedAccessException: Access to the path
'C:\Hosting\chek\demo.checks.in\wwwroot\web.config' is denied.
aspx.cs
string emailid = txtEmailid.Text.Trim();
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
config.AppSettings.Settings.Remove("kumarSunlightitMailid");
config.AppSettings.Settings.Add("kumarSunMailid",emailid);
config.Save();
lblNewEmailid.Text = ConfigurationManager.AppSettings["kumarSunMailid"] + " is ur new mail id";
web.config
<appSettings>
<add key="kumarSunMailid" value="krishna#gmail.com" />
</appSettings>
You are facing this error because you don't give read/write permissions to
user..
check couple of things
check wether web.config is readonly.. if so then uncheck that Option
Right Click on Web.config -> goto properties -> security and check you User does have read/write permissions. if not then give it read/write permissions
If you haven't tried this, try adding the Everyone User to that folder, and give him full access. Next, could try setting the configSource of appsettings so that these are stored in a separate config file:
<appSettings configSource="appSettings.config" />
And then create a new file in the same folder called appSettings.config:
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="yourKey" value="yourValue" />
</appSettings>
Try adding the Everyone user with full access to both files too.
Otherwise, I'd create a new solution an see if you can recreate this odd behavior there.
Please give permission to the current user to full access the iis project or solution.

Categories

Resources