I have implemented a winform application. I store the user settings for the application in the config file using the Configuration class.I store the exe along with the config file under the folder

C:\Users\\AppData\Local

This works fine in normal case, but I am facing problem in cases where user has redirected Appdata folder to some server address.In that case I get the exception:

Attempted to perform an unauthorized operation.

I found some similar questions here but none of them have any satisfactory answers.I tried to delete the config file before running the Configuration.Save command but that gives the exception:

The configuration file has been changed by another program.

So, how do I solve this problem. Here is my code to Update the config file:

string exePath = Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)), @"Local\<folder name>\<exe file name>");
Configuration configFile = ConfigurationManager.OpenExeConfiguration(exePath);
if (configFile.AppSettings.Settings[key] != null)
{
    configFile.AppSettings.Settings.Remove(key);
}
if (param)
{
    configFile.AppSettings.Settings.Add(key, value);
}
configFile.Save(ConfigurationSaveMode.Modified);

Here is the stack trace for the exception:

at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType
 type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)    
at System.Security.AccessControl.NativeObjectSecurity.Persist(String
 name, SafeHandle handle, AccessControlSections includeSections, Object
 exceptionContext)    
at System.Security.AccessControl.NativeObjectSecurity.Persist(String
 name, AccessControlSections includeSections, Object exceptionContext) 
at System.Security.AccessControl.NativeObjectSecurity.Persist(String
 name, AccessControlSections includeSections)    
at System.Security.AccessControl.FileSystemSecurity.Persist(String
 fullPath)    
at System.IO.File.SetAccessControl(String path, FileSecurity fileSecurity)    
at System.Configuration.Internal.WriteFileContext.DuplicateTemplateAttributes(String source, String destination)    
at System.Configuration.Internal.WriteFileContext.DuplicateFileAttributes(String source, String destination)    
at System.Configuration.Internal.WriteFileContext.Complete(String
filename, Boolean success)    
at  System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String
streamName, Boolean success, Object writeContext, Boolean assertPermissions)    at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)    
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)    
at
 System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)    
at System.Configuration.UpdateConfigHost.WriteCompleted(String
streamName, Boolean success, Object writeContext)    
at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename,
ConfigurationSaveMode saveMode, Boolean forceUpdateAll)    
at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)    
at UtilityClasses.ConfigurationHandler.UpdateConfigFile(String key, String value, Boolean param)
3

There are 3 answers

0
user1859022 On

I had a very similar expierience with changing a configuration file stored on a network share using ConfigurationManager.

It truns out that the Configuration.Save initially creates a temp file in the same location with the desired access set to Write DAC (Write Directory Access Control).

enter image description here

It even says so in the documentation:

When 'Creator Owner' is listed in the ACL (Access Control List) of the directory containing the configuration file, the current user of Save becomes the new owner of the file and inherits the permissions granted to 'Creator Owner'. This results in an elevation of privileges for the current user and a removal of privileges for the previous owner.

The only workaround I could find in my environment was to copy the config to local directory, alter the file locally and then copy it back.

2
Samvel Petrosov On

You have two ways to solve this problem:
1. Share network folder so that it does not autorization for users of the same network.
2. Use user credentials to pass authorization and connect to network path.

0
Dnyaneshwar Kanitkar On

Workarounds

  1. Check for a specific user's attribute on shared folder. it may have missing WriteExtended attribute.

  2. Check creator of shared folder which inherits all permissions from creator.

  3. Check if you can change configuration of your shared folder to non c: drive folder.