Work with SemaphoreSecurity : SemaphoreSecurity « Security « C# / CSharp Tutorial






using System;
using System.Threading;
using System.Security.AccessControl;
using System.Security.Principal;

public class Example
{
    public static void Main()
    {
        string user = Environment.UserDomainName + "\\" + Environment.UserName;
        SemaphoreSecurity mSec = new SemaphoreSecurity();
        SemaphoreAccessRule rule = new SemaphoreAccessRule(user, SemaphoreRights.Synchronize | SemaphoreRights.Modify, 
            AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        rule = new SemaphoreAccessRule(user, SemaphoreRights.ChangePermissions, AccessControlType.Deny);
        mSec.AddAccessRule(rule);

        ShowSecurity(mSec);

        rule = new SemaphoreAccessRule(user, SemaphoreRights.ReadPermissions, AccessControlType.Allow);
        mSec.AddAccessRule(rule);

        ShowSecurity(mSec);
    }

    private static void ShowSecurity(SemaphoreSecurity security)
    {
        foreach(SemaphoreAccessRule ar in 
            security.GetAccessRules(true, true, typeof(NTAccount)))
        {
            Console.WriteLine("        User: {0}", ar.IdentityReference);
            Console.WriteLine("        Type: {0}", ar.AccessControlType);
            Console.WriteLine("      Rights: {0}", ar.SemaphoreRights);
            Console.WriteLine();
        }
    }
}








35.17.SemaphoreSecurity
35.17.1.Work with SemaphoreSecurity