Tôi muốn viết mã kiểm tra quyền chia sẻ thư mục, tôi kiểm tra nhiều hơn một giải pháp nhưng nó hoạt động tốt khi cố gắng nhận quyền thư mục cục bộ nhưng khi tôi tạo các trường hợp kiểm tra cho thư mục được chia sẻ không thành công.Kiểm tra quyền chia sẻ thư mục - C#
Tôi cố gắng ví dụ trong câu hỏi này: SOF: checking-for-directory-and-file-write-permissions-in-net
nhưng nó chỉ hoạt động trên các thư mục địa phương.
Ví dụ, tôi sử dụng lớp này:
public class CurrentUserSecurity
{
WindowsIdentity _currentUser;
WindowsPrincipal _currentPrincipal;
public CurrentUserSecurity()
{
_currentUser = WindowsIdentity.GetCurrent();
_currentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
}
public bool HasAccess(DirectoryInfo directory, FileSystemRights right)
{
// Get the collection of authorization rules that apply to the directory.
AuthorizationRuleCollection acl = directory.GetAccessControl()
.GetAccessRules(true, true, typeof(SecurityIdentifier));
return HasFileOrDirectoryAccess(right, acl);
}
public bool HasAccess(FileInfo file, FileSystemRights right)
{
// Get the collection of authorization rules that apply to the file.
AuthorizationRuleCollection acl = file.GetAccessControl()
.GetAccessRules(true, true, typeof(SecurityIdentifier));
return HasFileOrDirectoryAccess(right, acl);
}
private bool HasFileOrDirectoryAccess(FileSystemRights right,
AuthorizationRuleCollection acl)
{
bool allow = false;
bool inheritedAllow = false;
bool inheritedDeny = false;
for (int i = 0; i < acl.Count; i++)
{
FileSystemAccessRule currentRule = (FileSystemAccessRule)acl[i];
// If the current rule applies to the current user.
if (_currentUser.User.Equals(currentRule.IdentityReference) ||
_currentPrincipal.IsInRole(
(SecurityIdentifier)currentRule.IdentityReference))
{
if (currentRule.AccessControlType.Equals(AccessControlType.Deny))
{
if ((currentRule.FileSystemRights & right) == right)
{
if (currentRule.IsInherited)
{
inheritedDeny = true;
}
else
{ // Non inherited "deny" takes overall precedence.
return false;
}
}
}
else if (currentRule.AccessControlType
.Equals(AccessControlType.Allow))
{
if ((currentRule.FileSystemRights & right) == right)
{
if (currentRule.IsInherited)
{
inheritedAllow = true;
}
else
{
allow = true;
}
}
}
}
}
if (allow)
{ // Non inherited "allow" takes precedence over inherited rules.
return true;
}
return inheritedAllow && !inheritedDeny;
}
}
Nó kiểm tra sự cho phép của mạo danh hiện hành về thư mục hoặc tập tin. Tất cả các trường hợp kiểm tra đều được truyền chính xác khi kiểm tra thư mục cục bộ nhưng một số trường hợp không thành công trong thư mục dùng chung, đó là vấn đề tôi muốn giải quyết, vì vậy có giải pháp nào không?
Các trường hợp thử nghiệm dưới đây thất bại mặc dù thư mục không có quyền ghi:
[TestMethod]
public void HasAccess_NotHaveAccess_ReturnsFalse()
{
CurrentUserSecurity cus = new CurrentUserSecurity();
bool result = cus.HasAccess(new DirectoryInfo(@"\\sharedpc\readonly"), System.Security.AccessControl.FileSystemRights.Write);
Assert.AreEqual(result, false);
}
Xin chào anh hùng, tôi đã thử mã của bạn và mọi thứ hoạt động tốt ở đây. Bạn có chắc chắn rằng bạn thiết lập quyền cho người dùng hiện tại trong thư mục "\\ sharedpc \ readonly" không? –
Xin chào, tôi đặt câu trả lời dưới đây nhưng nghĩ rằng đây là một điều môi trường hoặc chỉ là một chút nhầm lẫn. Tôi hiểu TestMethod của bạn trả về True cho biết người dùng * không * có quyền nhưng đó là * không chính xác *? 1. Bạn có thể [sửa] câu hỏi của bạn và cung cấp một ảnh chụp màn hình của các quyền truy cập thư mục và 2. cho biết tên tài khoản người dùng đang chạy mã 'WindowsIdentity.GetCurrent'. 3. Vui lòng xác nhận bạn đã thử nghiệm với WindowIdentity khác bên cạnh chính mình, cách dễ nhất để làm điều đó http://stackoverflow.com/questions/125341/how-do-you-do-impersonation-in-net/7250145#7250145 Cảm ơn . –
Bạn đã thử cách này chưa, https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemsecurity.accessrulefactory(v=vs.110).aspx? Nhận phương thức có thể sẽ không giải quyết các quy tắc lồng nhau hoặc các quy tắc phụ thuộc như quy tắc được áp dụng cho nhóm v.v., phương pháp này có thể cung cấp cho bạn quy tắc truy cập thực tế để xác minh. –