【发布时间】:2021-10-02 05:40:25
【问题描述】:
问题:
我正在关注microsoft reference,以便在网络共享上设置目录权限。
尽管 Intellicode 没有显示任何问题,但只要调用包含 dirInfo.GetAccessControl(); 的函数,我就会收到以下错误
System.MissingMethodException: '找不到方法:'System.Security.AccessControl.DirectorySecurity System.IO.DirectoryInfo.GetAccessControl()'。'
要执行的函数如下:
public static void SetFullPermission(string path, string domainAccount)
{
NTAccount ntaccount = new NTAccount("my_domain", domainAccount);
var ds = new DirectorySecurity();
DirectoryInfo dirInfo = new DirectoryInfo(path);
ds = dirInfo.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(ntaccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
ds.SetAccessRuleProtection(true, false); // disable inheritance and clear any inherited permissions
dirInfo.SetAccessControl(ds);
}
只要我取消注释 var ds = new DirectorySecurity(); 行,代码就会被执行,但没有设置权限。
代码参考
作为参考,以下代码可能有用。调用代码如下所示:
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("my_admin",pwd);
using (new NetworkConnection(@"\\my_fileserver\Data\Home", cred))
{
string path = $@"\\my_fileserver\Data\Home\testuserfolder";
string domainName = "my_domain";
string userNameToCreate = "testuser";
Directory.CreateDirectory(path);
SetFullPermission(path, userNameToCreate);
}
调用SetFullPermission 的代码利用了How to provide user name and password when connecting to a network share 中的一个类,因为我的普通帐户对网络驱动器没有权限。
类NewNetworkConnection()如下:
using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
namespace FileInterface
{
public class NetworkConnection : IDisposable
{
string _networkName;
public NetworkConnection(string networkName,
NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);
if (result != 0)
{
throw new Win32Exception(result);
}
}
~NetworkConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
}
解决方案参考
以下解决方案信息可能很重要:
- 解决方案有一个主项目
Ticketing solution目标.NET Core - 问题代码所依赖的项目是一个
.NET Framework类库,目标是.NET framework 4.8
不同框架目标的原因是某些代码需要一个框架,其他代码或引用可能只适用于另一个。
【问题讨论】:
标签: c# permissions acl