|
The Basics of Code Access Security by Thom Robbins
WSJ Vol 03 Issue 3 - pg.31
Listing 1: Imports System
Imports System.Security.Permissions
Imports System.Runtime.InteropServices
'The request is placed on the assembly level.
<assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum,
Flags := SecurityPermissionFlag.UnmanagedCode)>
Namespace MyNamespace
Public Class MyClass1
Public Sub New()
End Sub
Public Sub MyMethod()
'Perform interoperation with unmanaged code here.
End Sub
End Class
End Namespace
Listing 2: Generic implementation of role-based security
using System;
using System.Threading;
using System.Security;
using System.Security.Principal;
namespace RoleBasedSecurity
{
class Sample
{
static void Main(string[] args)
{
String [] roles = {"Lecturer", "Student"};
GenericIdentity i = new GenericIdentity("Thom");
GenericPrincipal g = new GenericPrincipal(i,
roles);
Thread.CurrentPrincipal = g;
if(Thread.CurrentPrincipal.Identity.Name ==
"Thom")
Console.WriteLine("Hello Thom");
if(Thread.CurrentPrincipal.IsInRole("Lecturer"))
Console.WriteLine("Hello Lecturer");
if(Thread.CurrentPrincipal.IsInRole("Employee"))
Console.WriteLine("Hello Employee");
}
}
}
|