C# 将权限分配给用户到可共享文件夹时出错;无法翻译部分或所有标识引用”;
我想创建一个窗口服务,它为用户提供对网络文件夹的权限,这意味着对于一个用户,它可能是读取选项,对于第二个写入选项或目录列表 我在测试阶段,所以创建了一个控制台应用程序。这是我的本地文件夹的工作。但是,当我更改代码并为我同事的可共享网络文件夹工作,并将权限分配给他的本地用户(如C# 将权限分配给用户到可共享文件夹时出错;无法翻译部分或所有标识引用”;,c#,active-directory,windows-services,window,windows-console,C#,Active Directory,Windows Services,Window,Windows Console,我想创建一个窗口服务,它为用户提供对网络文件夹的权限,这意味着对于一个用户,它可能是读取选项,对于第二个写入选项或目录列表 我在测试阶段,所以创建了一个控制台应用程序。这是我的本地文件夹的工作。但是,当我更改代码并为我同事的可共享网络文件夹工作,并将权限分配给他的本地用户(如Naveen\Bhavesh(而不是像网络服务、本地服务、everyone等这样的全局用户)时,上述错误就会抛出 我指的是我想要的,但解决方案不可理解。因此,任何机构都可以帮助纠正此代码 也请不要,我在局域网上工作,不是在a
Naveen\Bhavesh
(而不是像网络服务、本地服务、everyone等这样的全局用户)时,上述错误就会抛出
我指的是我想要的,但解决方案不可理解。因此,任何机构都可以帮助纠正此代码
也请不要,我在局域网上工作,不是在active directory上,但实际上这是为客户机,他们使用active directory,然后在那里实现这段代码。因此,也给出了与广告相关的建议
这是我的密码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.AccessControl;
namespace WindowUserRights
{
class Program
{
static void Main(string[] args)
{
//The below commented code is related to my pc and its working fine as I want.
//// Specify the directory you want to manipulate.
//string path = @"D:\\Project\\E\\Exercise\\WindowUserRights\\MyDir";
//string user1Read = "AJAYENDRA\\ASPNET";
//string user2Write = "AJAYENDRA\\Guest";
//string user3Write = "AJAYENDRA\\ReportUser";
//The below code is related to a shared folder of network pc and its gives the error when access the user.
// Specify the directory you want to manipulate.
string path = @"\\NAVEEN\Jobs";
string user1Read = "NAVEEN\\Bhavesh";
string user2Write = "NAVEEN\\Guest";
try
{
//string DirectoryName = path + "TestDirectory";
string DirectoryName = "";
DirectoryName = CheckAndCreateDirectory(path + "\\JobNo_user1Read_" + DateTime.Now.ToString("yyyyMMddHHmmssfff")); //check directory and create if does not exist;
// Add the access control entry to the directory.
Console.WriteLine("Adding access control entry for " + DirectoryName + " to user :- " + user1Read);
AddDirectorySecurity(DirectoryName, user1Read, FileSystemRights.Modify, AccessControlType.Allow);
DirectoryName = CheckAndCreateDirectory(path + "\\JobNo_user2Write_" + DateTime.Now.ToString("yyyyMMddHHmmssfff")); //check directory and create if does not exist;
Console.WriteLine("Adding access control entry for " + DirectoryName + " to user :- " + user2Write);
AddDirectorySecurity(DirectoryName, user2Write, FileSystemRights.ListDirectory, AccessControlType.Allow);
Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
//Check and create a directory, if does not exist.
public static string CheckAndCreateDirectory(string path)
{
// Specify the directory you want to manipulate.
//string path = @"D:\\Project\\E\\Exercise\\WindowUserRights\\MyDir";
try
{
// Determine whether the directory exists.
if (Directory.Exists(path))
{
Console.WriteLine("That path exists already.");
return "";
}
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
throw e;
}
//finally { }
return path;
}
// Adds an ACL entry on the specified directory for the specified account.
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
//the below line throw the error "some or all identity references could not be translated"
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
}
}