本文整理汇总了C#中System.IO.FileStream.GetAccessControl方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.GetAccessControl方法的具体用法?C# FileStream.GetAccessControl怎么用?C# FileStream.GetAccessControl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream.GetAccessControl方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAccessControl
public static FileSecurity GetAccessControl(FileStream fileStream)
{
return fileStream.GetAccessControl();
}
示例2: GetACL
//ACL分为自由访问控制列表(DACL)和系统访问控制列表(SACL),通过DACL可以控制对受保护资源的访问,SACL则可以控制受保护资源的系统审核策略
void GetACL()
{
string fileName = Server.MapPath("~/File/Test.doc");
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
FileSecurity fileSec = fs.GetAccessControl();
foreach (FileSystemAccessRule fileRule in fileSec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
{
Response.Write("File Name:" + fileName);
Response.Write("<br/>访问控制类型:" + (fileRule.AccessControlType == AccessControlType.Allow ? "允许访问" : "拒绝访问"));
Response.Write("<br/>访问控制权限:" + fileRule.IdentityReference.ToString());
}
}
}
示例3: InheritedPermissions
public void InheritedPermissions ()
{
AuthorizationRuleCollection rules;
DirectorySecurity dirSecurity; FileSecurity fileSecurity;
SecurityIdentifier usersSid = new SecurityIdentifier ("BU");
SecurityIdentifier worldSid = new SecurityIdentifier ("WD");
FileSystemAccessRule worldDirFullControl = new FileSystemAccessRule
(worldSid, FileSystemRights.FullControl,
InheritanceFlags.ObjectInherit, PropagationFlags.None,
AccessControlType.Allow);
if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
Assert.Ignore ();
}
string dirpath = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ());
string dirpath2 = null;
string filepath = null;
DirectoryInfo dirinfo = Directory.CreateDirectory (dirpath);
try {
// Set Full Control to Everyone.
dirSecurity = dirinfo.GetAccessControl ();
dirSecurity.SetGroup (usersSid);
dirSecurity.AddAccessRule (worldDirFullControl);
Directory.SetAccessControl (dirpath, dirSecurity);
// Did the rule store on the directory?
dirSecurity = Directory.GetAccessControl (dirpath);
rules = dirSecurity.GetAccessRules (true, false, typeof (SecurityIdentifier ));
Assert.AreEqual (usersSid, dirSecurity.GetGroup (typeof(SecurityIdentifier)));
Assert.AreEqual (1, rules.Count);
Assert.AreEqual (worldSid, rules[0].IdentityReference);
Assert.AreEqual (InheritanceFlags.ObjectInherit, rules[0].InheritanceFlags);
Assert.AreEqual (PropagationFlags.None, rules[0].PropagationFlags);
Assert.IsFalse (rules[0].IsInherited);
// Create a file. It will have no explicit rules.
filepath = Path.Combine (dirpath, Path.GetRandomFileName ());
using (FileStream file = new FileStream (filepath, FileMode.Create, FileAccess.ReadWrite)) {
fileSecurity = file.GetAccessControl ();
rules = fileSecurity.GetAccessRules (true, false, typeof (SecurityIdentifier));
Assert.AreEqual (0, rules.Count);
}
// Make sure the file has inherited the Full Control access rule.
FileInfo fileInfo = new FileInfo (filepath);
fileSecurity = fileInfo.GetAccessControl ();
rules = fileSecurity.GetAccessRules (false, true, typeof (SecurityIdentifier));
bool fileInheritedRule = false;
foreach (FileSystemAccessRule rule in rules) {
if (rule.AccessControlType == AccessControlType.Allow &&
rule.FileSystemRights == FileSystemRights.FullControl &&
rule.IdentityReference == worldSid &&
rule.IsInherited &&
rule.InheritanceFlags == InheritanceFlags.None &&
rule.PropagationFlags == PropagationFlags.None) // only containers get non-None flags
fileInheritedRule = true;
}
Assert.IsTrue (fileInheritedRule);
// ContainerInherit not being set, create a directory.
// Its inherited rule will have propagation flags to indicate only its children are affected.
dirpath2 = Path.Combine (dirpath, Path.GetRandomFileName ());
dirinfo = Directory.CreateDirectory (dirpath2);
dirSecurity = dirinfo.GetAccessControl ();
rules = dirSecurity.GetAccessRules (false, true, typeof (SecurityIdentifier));
bool dirInheritedRule = false;
foreach (FileSystemAccessRule rule in rules) {
if (rule.AccessControlType == AccessControlType.Allow &&
rule.FileSystemRights == FileSystemRights.FullControl &&
rule.IdentityReference == worldSid &&
rule.IsInherited &&
rule.InheritanceFlags == InheritanceFlags.ObjectInherit &&
rule.PropagationFlags == PropagationFlags.InheritOnly) // <-- key difference
dirInheritedRule = true;
}
Assert.IsTrue (dirInheritedRule);
} finally {
if (null != filepath) File.Delete (filepath);
if (null != dirpath2) Directory.Delete (dirpath2);
Directory.Delete (dirpath);
}
}