I am new to .net and C# and facing difficulty in modifying app.config.
In my app.config file, I have:
<UserInfo Domain = "domain" UserName = "Name" Password = "password">
Normally this information is given manually, when asked for, in the application. Is there anyway to modify the C# code such that this information is directly taken from an excel file with the required details. The application right now does not use or work with MS-excel.
If you wish to use excel file, then you have to parse the excel file using third party libraries. If that seems complex to you, use CSV file instead.
or Create a separate page for administering this username and password in your app. In that page save new username/password in web.config programmatically.
To know more see this question
Change a web.config programmatically with C# (.NET)
Related
I have been requested from my partner to use his API, and to use this API, I should encrypt all sent data to AES 256. He shared a .jks file with me, in addition to some parameters with values like (Alias, KEYSTORE_PASSWORD and KEY_PASSWORD), then he told me that the password which I should use for encryption is stored in that JKS file, and to open it, I should use the pre-shared parameters.
So, how can I reach that?
UPDATE ...
This is not a web service am trying to invoke, I just need to get the Password which is stored in the JKS file, so, I am not going to invoke an API or import a certificate into my client app. So, it doesn't matter if opening the app by C# or any other tool, i just need to get the password in order to use it later in encrypting some data.
I opened the given file by using KeyStore Explorer, then I imported the file into the app and providing it with all shared info like KeyStore_Password and Key_Password. Eventually, it opens.
I know that is away off C#, but all what I needed is to get the Password which is stored in that file, and this was my first time to deal with something like that.
But you cannot access "TrustedCertEntry". It is a restriction.
See: https://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#InstallProbs
We want to keep track of which User is logged in at the moment. Instead uf using for example: Environment.Username we want to know the Username from our database.
We are able to get the Username from the database but we want to store it somewhere. Any solutions?
An example from the question linked below:
Settings.Default["SomeProperty"] = "Some Value";
Settings.Default.Save(); // Saves settings in application configuration file
I recommend to have a glance at this question.
Best practice to save application settings. There are other solutions as well.
They are several solutions:
Using MVVM, create shared user manager service(recommended).
Singleton service.
Application settings.
Static variable.
If your application used by more than one user than you can store data in traditional file like, CSV, XML etc. But using such technique you may not get data security, so for that you could use Binary serialization.
I'm developing a .NET Framework 4.0 based Windows application.
I have a requirement of distributing this window application, along with source code to client.
When I test I'm using my own database credentials.
So I want a method to somehow hide the app.config details.
For this, I tried with encrypting values in app.config but faced an issue with token keys.
While researching about it, I found that I can use:
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration.
But that again required username and password for accessing remote server and don't want to show them to the client.
So for this I planned to read web.config hosted on IIS 7.5 Server.
Could you please help me in that context?
Or if you have better ideas to achieve the objective, do share.
The code is designed to be very close in syntax to the usual method used for accessing web.config from a web app. Pass the constructor the location of the web.config file you wish to parse and then use the AppSettings method to obtain the desired value;
string filename = #"c:\temp\Web.Config";
UK.Org.Webman.ConfigurationSettings ConfigurationSettings =
new UK.Org.Webman.ConfigurationSettings(filename);
string PrimaryDatabase = ConfigurationSettings.AppSettings["PrimaryDatabase"];
I have an application that reads an XML file for information on projects and displays them in a timelines. The user has the ability to modify and add projects, so I want to save this XML file.
I have a Silverlight application that displays the data, and a web project that hosts the XML file in it's ClientBin folder. The application gets the XML file by using the WebClient class:
WebClient dataSource = new WebClient();
dataSource.OpenReadCompleted += dataSource_OpenReadCompleted;
dataSource.OpenReadAsync(new Uri("ProjectData.xml", UriKind.Relative));
Then in the dataSource_OpenReadCompleted method it gets the stream from the e.Result object and reads it into an XDocument object which I parse using LINQ. This works fine.
Now I want to save my changes back to the web project. I have the modified XML in an XDocument object ready to go ... and I'm not sure how to write back.
There is a WebClient.OpenWriteAsync method, but I'm not sure how to use it. Googling doesn't give any clear results.
Thanks,
Andrew
Silverlight code runs on the client so...
You could try making a webservice that accepts the xml data and a file name. Then have that web service write the file back to the file system. Also please ensure that you use locks on the file so that multiple users don't try to write to the file at the same time.
Thanks for the suggestion kurtnelle, that would also work. I figure I'll write what my solution was for anyone who stumbles upon this question.
One cannot use OpenWriteAsync the same way you use OpenReadAsync because for security reasons Silverlight cannot write directly to the file system.
I ended up using an HTTPHandler method in the web project. I created a file called XMLHandler.ashx that listened for a call to the webclient. In the Silverlight app I called webclient.UploadStringAsync with the URI of the web project and the xml file as the data string. When the HTTPHandler hears this, it saves it to the client bin. It works perfectly!
What is the best practice to store application settings (such as user name and password, database location ...) in C# ?
Hint: I am new to .net and C#
Application Configuration Settings that are application wide (non-user specific) belong in either app.config (for Desktop apps) or web.config (for Web apps).
Encrypting sections of a web.config file is quite simple as outlined in this Super Simple Example.
If you need to store User specific settings (like application settings, etc.) or Application wide settings not related to application configuration you can use a Settings file as described here:
User Settings in C#
I'm not sure what version of .net/Visual Studio it was introduced in, but you can right click on your project, choose 'Add New Item' and select 'Settings File' from the "Add New Item" window. This provides your project with a (named by default) Settings.settings file that you can configure all the settings you want to expose in.
You can define settings that you create to be either Application or User which means you can use this single interface to control global and user settings. Once you've created a setting in the Settings.settings file using the editor that Visual Studio provides, you can access it in code like this:
// Get a Setting value
var valueOfSetting1 = Settings1.Default.Setting1;
// Modify and save a Setting value
Settings1.Default.Setting1 = "New Value";
Settings1.Default.Save();
First option is the registry. It is easy, but it is not too safe for passwords. Another option is using a file that you create. This too isn't safe, unless you want to implement cryption.
Next option is using the Application Settings. This is also quite simple, but there are a few catches. First, right click on your project and go to Properties. There, under the Settings tab, you can store variables to which you can access from your program by
string password = Properties.Settings.Default.Password
You can also change them the same way, but ONLY IF the scope is set the User. WHen the scope is application-wide, VS does not allow you to change these variables for some odd reason. To save changes, you must call Save() as follows:
Properties.Settings.Default.Save();
These are saved in the User Data folder under C:\Documents and Settings\'Current User'\Local Settings\Application Data\
Another option would be to include them in your database, but since you are also storing your database location, this might not work for you.
I think app.config (non web app) or web.config (web app).
These sorts of settings usually land in Application Configuration Files (web.config, app.config).
http://www.devasp.net/net/articles/display/679.html
If you are storing passwords, you might also need to encrypt the configuration section in question.
http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx
http://msdn.microsoft.com/en-us/library/ff650304.aspx
Note if you use app.config, you will see it get renamed to ..config, depending on if your output produces a DLL or an EXE.
As with the above replies suggest, app.config or the web.config is the best place for app settings.
If you need a more robust way of xml style tags for database, server settings and the like, you can use the configurationSection and create custom sections.
http://www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx
For database passwords, the way i do it is have an encrypted string in the xml tag value and decrypt then when reading them, that way you dont expose the passwords.
appsettings config file, ini file(nini), embeddable database(sqlite,berkelydb/etc..),whatever method you like, it depends on your application size/performance consideration and design.