Declarative role-based security : Role « Security « C# / CSharp Tutorial






using System;
using System.IO;
using System.Security.Permissions;

[PrincipalPermissionAttribute(SecurityAction.Demand, Role="Administrators")]
class MainClass
{
  public static void Main() 
  {
    FileStream fsOut = File.Create(@"c:\\temp\\test.txt");
    StreamWriter sw = new StreamWriter(fsOut);
    sw.WriteLine("str");
    sw.Flush();
    sw.Close();
  }
}
Unhandled Exception: System.Security.SecurityException: Request for principal permission failed.
   at System.Security.Permissions.PrincipalPermission.ThrowSecurityException()
   at System.Security.Permissions.PrincipalPermission.Demand()
   at System.Security.PermissionSet.DemandNonCAS()
   at MainClass.Main()
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.PrincipalPermission
The first permission that failed was:




The demand was for:




The assembly or AppDomain that failed was:
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089








35.2.Role
35.2.1.Checking for access in a role
35.2.2.Declarative role-based security