Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#
Tip/Trick

How can we get the security access information.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
5 Jul 2010CPOL 10.5K   8  
For programmers it is more important that you identify whether you are allowed to do something.

Introduction


One of the most important tasks is to getting and setting the security access for all the administrator. For programmers it is more important that you identify whether you are allowed to do something.

Let’s start to learn how we can get the security access information. The System.Security.Permissions namespace contain a number of different kinds of permissions. For this purpose we use the following

Class:
(i) System.Security.Permissions.RegistryPermission.
(ii) System.Security.Permissions.UIPermission.
Enum:
(i) System.Security.Permissions.FileIOPermissionAccess.

For more information about System.Security.Permissions can be found at this link.[^]

A sample code is given below:

Code


C#
public class SecurityAccessExample
{
    public void GetSecurityAccess()
    {
        System.Security.Permissions.RegistryPermission _objRegistryPermission
                = new RegistryPermission(RegistryPermissionAccess.Read, "HARDWARE\\DESCRIPTION\\SYSTEM\\0");
        try
        {
            // verify for permission to read the registry key
            if (_objRegistryPermission.IsUnrestricted())
            {
                Console.WriteLine("Unrestricted Access ALLOWED.");
            }
            else
            {
                Console.WriteLine("Unrestricted Access DENIED.");
            }
            // Verify clipboard access
            System.Security.Permissions.UIPermission _objUIPermission
                = new UIPermission(UIPermissionWindow.AllWindows, UIPermissionClipboard.AllClipboard);
            if (_objUIPermission.IsUnrestricted())
            {
                Console.WriteLine("UI Unrestricted Access ALLOWED.");
            }
            else
            {
                Console.WriteLine("UI Unrestricted Access DENIED.");
            }
            // Verify local files access
            Console.WriteLine("Local files read access: {0}"
                           , System.Security.Permissions.FileIOPermissionAccess.Read);
            Console.WriteLine("Local files write access: {0}"
                           , System.Security.Permissions.FileIOPermissionAccess.Write);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --