October 2007 - Posts

Tiny Tip: Improve Security with InternalsVisibleTo Attribute

In projects that span over multiple teams and for other very reasonable architecture reasons you end up with multiple assembles for your current project or even if you want to reuse a library one of the other teams wrote you will find yourself using code from other assembly, now you have couple of choices A. design the methods you want to expose to other assemblies as public methods for them to be able to access but be aware that once they are public even a comparator can get benefit from the same library you invested in. B. you can use the InternalsVisibleTo attribute.

You can (Optional) improve your security by adding a PuplicKey/PublicKeyToken to the InternalsVisibleTo attribute assembly definition by doing this you will tell the compiler to enforce that any assembly want to gain access to these internal types must have a matching key. 

What InternalsVisibleTo dos is that it ties your assembly to the valuable library using assembly public key so you can access it's internal types.

the good news is it's very very easy to implement, you just must have a strong name assembly in place. Lets say we have two dev teams TeamA and TeamB, TeamA develop this assembly that later on one of the TeamB projects they will need the same functionality, this code snippet will show you how to allow external assembly to access internal types on another.

//AssemblyInfo.cs

Using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("CriticalSites, PublicKeyToken=123456789")]

not only that but you can also enforce a version number of the assembly so that a client assembly can only bind to the right version.

[assembly:InternalsVisibleTo("CriticalSites, PublicKeyToken=123456789,Version=1.2.3.4")]

Posted by Adel Khalil with 6 comment(s)
Filed under: , , ,

Tiny Tip: is vs. as

Today's tip is a quick look at both not widely used C# operators and why prefer one to another..

Casting using is-operator: 

if(p is Product) // CLR?: Could i cast this to product?

{

      Product x = (Product)p; // CLR again??: Could i cast this to Product?

}

This work but comes with performance cost so you better use the as-operator as follows:

Casting using as-operator:

Product x = p as Product; // CLR?: Could i cast this to product?

if(x!=null)

{

     // your logic here..

}

Here the CLR checks for type compatibility only one time and as-operator never throw an exception only the result will be null if it's not type compatible.

Posted by Adel Khalil with 5 comment(s)
Filed under: , , ,

Mission Impossible: Opening .NET 1.1/VS 2003 Web Project

mi4

If you are lucky enough to not work with web project in VS 2003 don't read on.

i have a massive amount of time trying to figure how to open a web project written in .NET 1.1/VS 2003 on another machine on couple of XP  machines every time i got error and try to hack into it to get another one, all kind of error message you name it, tried everything news groups, forums even cached expert-exchange :) with no luck.. not because no one out there have similar experience but because tons of people have but it seems like this problem have like million cause and millions of solution and what works for you not guaranteed to work with your peers so here i add mine the one works for me that i didn't find maybe will be helpful for the next poor developer.

1-Uninstall IIS 5.1

2-Restart

3-Uninstall Visual Studio 2003

4-Uninstall Microsoft .NET Framework 1.1 (don't think this matter but what the hell throw that too)

5-Restart again

6-Install IIS 5.1 + Front Page Server Extensions

7-Configure Server Extensions for the default website

8-Restart for the third time

9-Install Visual Studio 2003

10-Create a virtual directory for your solution on IIS

11-go to the solution folder and open .sln file with notepad you will find something similar to this

Microsoft Visual Studio Solution File, Format Version 8.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Project)Name",
http://localhost/Solution_Folder/Project_Folder/MyProject.csproj, "{70B6AF8F-5AAE-4E9A-A0C5-F279A3326811}"
    ProjectSection(ProjectDependencies) = postProject
    EndProjectSection
EndProject

map the path to the actual .csproj file for your solution.

12-Smile smile_regular

Posted by Adel Khalil with 1 comment(s)

Tiny Tip: Make sure your feed is discovered by IE7 RSS Button

To do so just add this little snippet of code to your default page and you good to go.

<head>
    <link rel="alternate" type="application/rss+xml" title="Live From Cairo"
    href="http://bloggingabout.net/blogs/adelkhalil/default.aspx">
</head>
Posted by Adel Khalil with 2 comment(s)