Permissions på et share - windows 7
Hejsajeg har fundet nedenstående kode, som opretter permissions til et share. Jeg kan godt få det til at virke - bortset fra at brugeren alligevel ikke kan tilgå sharet. Brugeren dukker op i listen under *folder*>properties>sharing>advanced sharing>permissions - og med de rigtige afhakninger. Brugeren findes ikke under *folder*>properties>sharing>share - hvilket undre mig lidt.
Nogen gode ideer?
//Create a new Win32_Ace instance. Please refer to my previous post about creating Win32_Ace.
NTAccount account = new NTAccount("domain", "username");
SecurityIdentifier sid = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
byte[] sidArray = new byte[sid.BinaryLength];
sid.GetBinaryForm(sidArray, 0);
ManagementObject Trustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null);
Trustee["Domain"] = "domain";
Trustee["Name"] = "username";
Trustee["SID"] = sidArray;
ManagementObject ACE = new ManagementClass(new ManagementPath("Win32_Ace"), null);
ACE["AccessMask"] = 1179817;
ACE["AceFlags"] = 3;
ACE["AceType"] = 0;
ACE["Trustee"] = Trustee;
//After we have the new Win_32Ace, now we need to get the existing Ace instances (DACL).
//Create an instance of Win32_LogicalSecuritySetting, set the path to the server and the share.
ManagementObject Win32LogicalSecuritySetting = new ManagementObject(@"\\tobipc\root\cimv2:Win32_LogicalShareSecuritySetting.Name='tester'");
//Call the GetSecurityDescriptor method. This method returns one out parameter.
ManagementBaseObject Return = Win32LogicalSecuritySetting.InvokeMethod("GetSecurityDescriptor", null, null);
//The return value of that call above has two properties, ReturnValue, which you can use
//to read the status of the call (failed, success, etc.), and Descriptor, which is an instance
//of Win32_SecurityDescriptor.
Int32 ReturnValue = Convert.ToInt32(Return.Properties["ReturnValue"].Value);
if (ReturnValue != 0)
throw new Exception(String.Format("Error when calling GetSecurityDescriptor. Error code : {0}.", ReturnValue));
//Retrieve the array of DACL from the Security Descriptor.
ManagementBaseObject SecurityDescriptor = Return.Properties["Descriptor"].Value as ManagementBaseObject;
ManagementBaseObject[] DACL = SecurityDescriptor["DACL"] as ManagementBaseObject[];
if (DACL == null)
DACL = new ManagementBaseObject[] { ACE };
else
{
Array.Resize(ref DACL, DACL.Length + 1);
DACL[DACL.Length - 1] = ACE;
}
//Reassign the new DACL array with the new user Ace back to the Win32_SecurityDescriptor instance, and call the
//SetSecurityDescriptor method.
SecurityDescriptor["DACL"] = DACL;
foreach (ManagementBaseObject mbo in DACL)
{
string test = mbo["AccessMask"].ToString();
}
ManagementObject Share = new ManagementObject(@"\\tobipc\root\cimv2:Win32_Share.Name='tester'");
ReturnValue = Convert.ToInt32(Share.InvokeMethod("SetShareInfo", new object[] {Int32.MaxValue, "Dataplatformshare", SecurityDescriptor}));
if (ReturnValue != 0)
throw new Exception(String.Format("Error when calling GetSecurityDescriptor. Error code : {0}.", ReturnValue));
}