一个用户写入文件,另一个用户访问失败

本文关键字:用户 文件 另一个 失败 访问 一个 | 更新日期: 2023-09-27 17:57:59

我有两个程序,每个程序都作为单独的帐户运行,尽管两者都是Admin帐户。第一个是加密文件的Windows窗体应用程序,另一个是Windows服务。我有一个文件,我使用Windows 8机器上的标准管理帐户从Windows窗体程序进行加密。如果我向c:''test.xml这样的位置写入,它似乎可以工作。但是,在安装程序将所有这些安装到一个目录的生产中不能使用。

然后,我尝试读取并解密同一目录中的同一文件,使用第二个程序,一个Windows服务,在一个名为"Admin"的不同帐户下运行。问题是,当我尝试执行File.ReadAllText时,会出现Access Denied错误。AES加密是否使用本地计算机证书存储,因为我想知道那里是否存在权限问题?

WinForms代码(在标准管理员帐户下运行):

string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");
// var fileName = @"c:/text.xml";
XDocument doc = new XDocument();
XElement xml = new XElement("Info",
    new XElement("DatabaseServerName", txtServerName.Text),
    new XElement("DatabaseUserName", txtDatabaseUserName.Text),
    new XElement("DatabasePassword", txtDatabasePassword.Text),
    new XElement("ServiceAccount", txtAccount.Text),
    new XElement("ServicePassword", txtServicePassword.Text),
    new XElement("RegistrationCode", txtRegistrationCode.Text));
doc.Add(xml);
//using (var aes = Aes.Create())
//{
//    aesKey = aes.Key;
//    key = Convert.ToBase64String(aes.Key);
//}
string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
var aesKey = Convert.FromBase64String(sKey);
string encyptedText = EncryptDecrpt.EncryptStringToBase64String(doc.ToString(), aesKey);
File.WriteAllText(fileName, encyptedText);

Windows服务代码(在帐户管理员下运行):

string path = AppDomain.CurrentDomain.BaseDirectory;
eventLog1.WriteEntry(path);
string fileName = System.IO.Path.Combine(path, "alphaService.xml");
// var fileName = @"c:/text.xml";
string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
Byte[] keyBytes = Convert.FromBase64String(sKey);
var encryptedText = File.ReadAllText(fileName, new ASCIIEncoding());
string xmlStr = DecryptStringFromBase64String(encryptedText, keyBytes);

我还尝试以管理员身份运行Windows窗体。我尝试将用户"Admin"添加到文件ACL,但我得到了相同的访问被拒绝,

一个用户写入文件,另一个用户访问失败

您可能已经成为用户帐户控制数据重定向的受害者。由于Windows Vista,Windows不允许写入某些目录,并且会自动将任何尝试重定向到每个用户的虚拟存储。您的应用程序创建的文件可能在下的某个位置结束

C: ''用户''<用户名>''AppData''Local''VirtualStore

windows服务找错地方了,因为它是在不同的用户帐户下运行的。