【问题标题】:Programmatically adding security permissions to files in C#以编程方式向 C# 中的文件添加安全权限
【发布时间】:2010-11-05 09:55:20
【问题描述】:

在一个 asp.net 应用程序中,我有一个任务是将一些 xml 文件传输到我计算机上的本地文件夹中。然后我想读取这些文件,但是当它们被复制到我的本地文件夹时,它们没有设置网络服务用户帐户。所以,我的问题是,在 .Net C# 中,您如何以编程方式将具有完全控制权的“网络服务”帐户添加到我的 xml 文件中。

【问题讨论】:

    标签: c# asp.net windows file permissions


    【解决方案1】:

    FileSecurity class in MSDN

    以下代码示例使用 FileSecurity 类在文件中添加和删除访问控制列表 (ACL) 条目。您必须提供有效的用户或组帐户才能运行此示例。

    using System;
    using System.IO;
    using System.Security.AccessControl;
    
    namespace FileSystemExample
    {
        class FileExample
        {
            public static void Main()
            {
                try
                {
                    string fileName = "test.xml";
    
                    Console.WriteLine("Adding access control entry for "
                        + fileName);
    
                    // Add the access control entry to the file.
                    AddFileSecurity(fileName, @"DomainName\AccountName",
                        FileSystemRights.FullControl, AccessControlType.Allow);
    
                    Console.WriteLine("Removing access control entry from "
                        + fileName);
    
                    // Remove the access control entry from the file.
                    RemoveFileSecurity(fileName, @"DomainName\AccountName",
                        FileSystemRights.FullControl, AccessControlType.Allow);
    
                    Console.WriteLine("Done.");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
    
            // Adds an ACL entry on the specified file for the specified account.
            public static void AddFileSecurity(string fileName, string account,
                FileSystemRights rights, AccessControlType controlType)
            {
    
    
                // Get a FileSecurity object that represents the
                // current security settings.
                FileSecurity fSecurity = File.GetAccessControl(fileName);
    
                // Add the FileSystemAccessRule to the security settings.
                fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                    rights, controlType));
    
                // Set the new access settings.
                File.SetAccessControl(fileName, fSecurity);
    
            }
    
            // Removes an ACL entry on the specified file for the specified account.
            public static void RemoveFileSecurity(string fileName, string account,
                FileSystemRights rights, AccessControlType controlType)
            {
    
                // Get a FileSecurity object that represents the
                // current security settings.
                FileSecurity fSecurity = File.GetAccessControl(fileName);
    
                // Remove the FileSystemAccessRule from the security settings.
                fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                    rights, controlType));
    
                // Set the new access settings.
                File.SetAccessControl(fileName, fSecurity);
    
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果有帮助,试试这个代码

          public static bool CheckReadWriteAccces(string filePath, System.Security.AccessControl.FileSystemRights fileSystemRights)
          {
              FileInfo fileInfo = new FileInfo(filePath);
      
              string str = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToUpper();
              foreach (System.Security.AccessControl.FileSystemAccessRule rule in fileInfo.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
              {
                  if (str == rule.IdentityReference.Value.ToUpper())
                      return ((rule.AccessControlType == System.Security.AccessControl.AccessControlType.Allow) && (fileSystemRights == (rule.FileSystemRights & fileSystemRights)));
              }
      
              return false;
          }
      
      
          /// <summary>
          /// Make a file writteble
          /// </summary>
          /// <param name="path">File name to change</param>
          public static void MakeWritable(string path)
          {
              if (!File.Exists(path))
                  return;
              File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
          }
      

      【讨论】:

        【解决方案3】:
        猜你喜欢
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-04
        • 1970-01-01
        • 2012-02-07
        • 1970-01-01
        • 2018-05-25
        相关资源
        最近更新 更多