Hello!
I have a simple application from a book where the actual applications
permission doesn't match the permission referenced from the book.
My question is written further down.
The book is saying the following : "The calculate permission button from the
security properties of Visual Studio analysis is
an application manifest that includes all required permissions. With Visual
Studio 2005, you can see the
application manifest with the name app.manifest below Properties in the
Solution Explorer. The content of this file is shown
here. The XML element <applicationRequestMinimumdefines all required
permissions of the application.
The FileIOPermission is required because the application reads and writes
files using classes from System.IO namaspace."
Below is a copy of app.manifest.
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0"
xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom" />
<PermissionSet class="System.Security.PermissionSet" version="1"
ID="Custom" SameSite="site">
<IPermission
class="System.Security.Permissions.ReflectionPermi ssion, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Unrestricted="true" />
<IPermission
class="System.Security.Permissions.SecurityPermiss ion, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Flags="UnmanagedCode, Execution, ControlEvidence" />
<IPermission class="System.Security.Permissions.UIPermission,
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Unrestricted="true" />
<IPermission
class="System.Security.Permissions.KeyContainerPer mission, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Unrestricted="true" />
</PermissionSet>
</applicationRequestMinimum>
</security>
</trustInfo>
</asmv1:assembly>
Below is the main class and as you can see I use file IO operation so the
application needs the FileIOPermission.
Now to my question: When I click on the "calculate Permission" button in the
sequrity after choosing the property for the
project the FileIOPermission is not selected as a requirement for the
application but according to the book the appliaction
needs this requirement and I must agree with the book in this case.
Can anyone explain the reason why the sequrity button named calculate
permission doesn't include FileIOPermission for
the application as a permission requirement ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace SimpleEditor
{
public partial class SimpleEditorForm : Form
{
private string filename = "Untitled";
private string[] lines;
private int linesPrinted;
private Brush printBrush;
public SimpleEditorForm() //User defined C-tor
{ InitializeComponent(); }
protected void OpenFile()
{
try
{
textBoxEdit.Clear();
textBoxEdit.Text = File.ReadAllText(filename);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void OnFileNew(object sender, EventArgs e)
{
filename = "Untitled";
SetFormTitle();
textBoxEdit.Clear(); // clear textbox
}
private void OnFileOpen(object sender, EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
filename = dlgOpenFile.FileName;
SetFormTitle();
OpenFile();
}
}
private void OnFileSave(object sender, EventArgs e)
{
if (filename == "Untitled")
OnFileSaveAs(sender, e);
else
SaveFile();
}
private void OnFileSaveAs(object sender, EventArgs e)
{
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
filename = dlgSaveFile.FileName;
SetFormTitle();
SaveFile();
}
}
private void SaveFile()
{
try
{
File.WriteAllText(filename, this.textBoxEdit.Text);
}
catch(IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor",
MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon. Hand);
}
}
protected void SetFormTitle()
{
Text = new FileInfo(filename).Name + "- Simple Editor";
}
private void OnFilePrint(object sender, EventArgs e)
{
if (this.textBoxEdit.SelectedText != "")
dlgPrint.AllowSelection = true;
if (dlgPrint.ShowDialog() == DialogResult.OK)
printDocument.Print();
}
private void OnFilePrintPreview(object sender, EventArgs e)
{ dlgPrintPreview.ShowDialog(); }
private void OnFilePageSetup(object sender, EventArgs e)
{ dlgPageSetup.ShowDialog(); }
private void OnExit(object sender, EventArgs e)
{ Application.Exit(); }
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
while (linesPrinted < lines.Length) //antal rader att printa
{
e.Graphics.DrawString(lines[linesPrinted++],
this.fontDialog.Font, printBrush, x, y);
y += textBoxEdit.Font.Height;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
}
}
private void OnBeginPrint(object sender, PrintEventArgs e)
{
char[] param = { '\n' };
if (dlgPrint.PrinterSettings.PrintRange == PrintRange.Selection)
lines = textBoxEdit.SelectedText.Split(param);
else
lines = textBoxEdit.Text.Split(param);
//int i = 0;
//char[] trimParam = { '\r' };
//foreach (string s in lines)
// lines[i++] = s.TrimEnd(trimParam);
if (this.dlgPrint.PrinterSettings.SupportsColor)
printBrush = new SolidBrush(textBoxEdit.ForeColor);
else
printBrush = Brushes.Black;
}
private void OnEndPrint(object sender, PrintEventArgs e)
{ lines = null; }
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog.ShowDialog() == DialogResult.OK)
textBoxEdit.Font = fontDialog.Font;
}
private void colorToolStripMenuItem_Click(object sender, EventArgs
e)
{
if (colorDialog.ShowDialog() == DialogResult.OK)
textBoxEdit.ForeColor = colorDialog.Color;
}
}
}
//Tony