Add access to your files with DirectoryServices

xiaoxiao2021-03-06  202

Using system;

Using system.collections;

USING ACTIVEDS;

Namespace pardesiservices.fixfilepermission

{

Class FileSecurity

{

[Stathread]

Static

Void main

String [] ARGS)

{

String strfile = @

"D: /mmcinst.log";

Try

{

AdssecurityUtilityClass Secuutil =

New AdssecurityUtilityClass ();

Object ob = secuutil.getsecurityDescriptor

Strfile,

(

int) activeds.ads_pathtype_enum. ADS_PATH_FILE,

(

INT) activeds.ads_sd_format_enum.ads_sd_format_iid);

IF

NULL! = OB)

{

ActiveDs.iadssecurityDescriptor SD =

(IadssecurityDescriptor) OB;

ActiveDs.iadsaccessControlList OBDACL =

(Activeds.iadsaccessControllist) SD.DISCRETITYAACL;

Bool bagdace =

True;

Ienumerator Obacenum = OBDACL.GETENUMERATOR ();

While (Obacenum.Movenext ())

{

IadsaccessControLENTRY OBACE =

IadsaccessControllentry OBACEENUM.CURRENT;

Console.writeline

"Trustee: {0}", OBACE.TRUSTEE;

// Check if "ASPNET" Account is trustee of ace or not.

IF (OBACE.TRUSTEE.INDEXOF

"ASPNET")! = -

1)

{

// Check if this is a alowed ace or not.

IF (OBACE.ACETYPE ==

(

INT) ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED

{

Baddace =

False;

}

}

}

// if BadDace Flag IS Set, Then We will add it.

IF (bottom)

{

AccessControLENTRYCLASS OBNEWACE =

New AccessControLEntryClass ();

Obnewace.Actype =

(

INT) ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED;

Obnewace.trustee = @

ASPNET ";

Obnewace.accessmask = -

1;

OBDACL.Addace (Obnewace);

SD.DISCRETIONAACL = OBDACL;

Secuutil.SetSecurityDescriptor

Strfile,

(

INT) ADS_PATHTYPE_ENUM.ADS_PATH_FILE,

SD,

(

INT) ADS_SD_FORMAT_ENUM.ADS_SD_FORMAT_IID);

}

}

}

Catch (Exception EX)

{

Console.writeline (ex.Message);

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-128629.html

New Post(0)