Writing secure code. : PermissionSet « Security « C# / C Sharp






Writing secure code.

 


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security;
using System.IO;
using System.Security.Permissions;

public class MainClass {
    public static void Main(){
        PermissionSet ps = new PermissionSet(System.Security.Permissions.PermissionState.None);

        ps.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read |FileIOPermissionAccess.Write | FileIOPermissionAccess.Append,"c:\\temp\\temp.txt"));

        ps.Deny();
        //ps.Assert();

        FileStream fs = new FileStream(@"c:\temp\temp.txt",FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter m_streamWriter = new StreamWriter(fs);

        m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
        m_streamWriter.Write("Test");
    }
}

 








Related examples in the same category