Resources

Assemblies, Configuration and Security

Lesson 1 - Assemblies and Resources

Assemblies

[assembly: AssemblyTitle("")]

[assembly: AssemblyDescription("")]

[assembly: AssemblyConfiguration("")]

...

Class Library Assemblies

Resources and Resource Assemblies

Satellite Assemblies

Retrieving Resources

// Obtain resources from same assembly 
ResourceManager myManager = new ResourceManager("myNamesapce.myResources", this.GetType().Assembly);

// Obtain resources from another assembly
System.Reflection.Assembly myResources;

myResources = System.Reflection.Assembly.Load("ResAssembly");

ResourceManager myManager = new ResourceManager("myNamesapce.myResources", myResources);

Shared Assemblies

sn -k myKey.snk
Assembly: AssemblyKeyFile("..\\..\\myKey.snk");
gacutil /i myAssembly.dll

Lesson 2 - Configuration and Optimisation

The Configuration File

Dynamic Properties

Using Properties Window To Configure Properties

<add key="Button1.Text" value="Button1">

appears in config file to configure Text attribute of Button1 control.

Setting / Retrieving Dynamic Properties Manually

//Create AppSettingsReader 
System.Configuration.AppSettingsReader myReader = new System.Configuration.AppSettingsReader();

// Create a widget
Widget myWidget = new Widget();

// Retrieve dynamic property
myWidget.Text = myReader.GetValue("DynamicWidget.Text", typeof(System.String).ToString());
<appSettings>

<add key="Widget.Visible" value="True"/>

<add key="Widget.Text" value="Hello, world"/>

</appSettings>

Optimise performance

  1. Measure performance data
  2. Identify bottlenecks
  3. Tune code
  4. Repeat

Measuring performance

Compiler Optimisations

Lesson 3 - Security

Permissions

Role Based Authorisation

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrinciplal);
WindowsPrincipla myPrincipal;

myPrincipal = (WindowsPrincipal)

System.Threading.Thread.CurrentPrincipal;

WindowsIdentity myIdentity;

myIdentity = (WindowsIdentity) myPrincipal.Identity;

MessageBox.Show(myIdentity.Name);

Imperative Security

// Create principal permission for manager named megan 
PrincipalPermission myPermission = new PrincipalPermission("Megan", "Manager");

// Ensure current user is manager named megan
myPermission.Demand();
// Create principal permission for manager named megan 
PrincipalPermission Permission1 = new PrincipalPermission("Megan", "Manager");

// Create principal permission for group manager named
// ann
PrincipalPermission Permission2 = new PrincipalPermission("Ann", "Group Manager");

// Create union
PrincipalPermission Permission3 = (PrincipalPermission) Permission2.Union(Permission1);

// Ensure current user is manager named megan and
// group manager named ann
Permission3.Demand();
// Create permission to check user is a manager 
PrincipalPermission myPermission = new PrinicpalPermission(null, "Manager");
// Create permission to check user is an Administrator 
PrincipalPermission myPermission = new PrinicpalPermission(null, "BUILTIN\\Administrators");

Declarative Security

[PrincipalPermission(SecurityAction.Demand, Name="Joe", Role="Clerk")] 
public void MyMethod()
{

}

Code Access Security

FileIOPermission myPermission = new FileIOPermission( FileIOPermissionAccess.Write, "C:\\myFile.txt");
ReflectionPermission myPermission = new ReflectionPermission(PermissionState.Unrestricted);

UIPermission anotherPermission = new UIPermission(PermissionState.None);

Imperative Security

// Create permission object representing unrestricted 
// access to file system
FileIOPermission myPermission = new FileIOPermission(Permissionstate.Unrestricted);

// Verify all callers to code have unrestricted access
// to file system
myPermission.Demand();
// Create permission object representing unrestricted 
// access to file system
FileIOPermission myPermission = new FileIOPermission(Permissionstate.Unrestricted);

// Denies access to the file system from this method
myPermission.Deny();
// Create permission object representing only write 
// access to c:\myFile.txt
FileIOPermission myPermission = new FileIOPermission(FileIOPermission.Write, "C:\\myFilt.txt");

// Only allow write access to c:\myFile.txt, deny all
// else
myPermission.PermitOnly();
// Create permission object representing unrestricted 
// access to file system
FileIOPermission myPermission = new FileIOPermission(Permissionstate.Unrestricted);

// Assert that this method has unrestricted access to
// file system
myPermission.Assert();

Declarative Security

// Deny fileIOPermission to class 
[FileIOPermission(SecurityAction.Deny)]
public class aClass
{

}
[assembly: FileIOPermission(SecurityAction.RequestMinimum)]
[FileIOPermission(SecurityAction.Assert, Write="C:\\myFile.txt")] 
public void WriteFile()
{

}

Exception Handling and Imperative Security

Downloads