Declarative role-based security : Role : Security C# Examples


C# Examples » Security » Role »

 

Declarative role-based security









    
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();
    }
}
    
   
  
   



Output

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


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Security
» Role