Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

The .NET Framework Security Model

4.29/5 (10 votes)
28 Apr 20061 min read 1  
An article on the .NET framework security model.

Introduction

When I was first faced with the .NET Framework security and cryptography fist, I noticed that most of the quick-start articles sufferes from lots of low-level details and explanations of the nuts and bolts of .NET Framework Security.

But for the first time, I needed a brief, easy to understand and remember tutorial, instead. So, actually this is not an article. This is rather a bird's-eye overview of the .NET Framework security model, with visual illustrations.

Security concepts

You can implement the security model in your applications in a declarative and/or an imperative way. In most cases, you should prefer declarative security, and implement the security model only when some application security issues can be known only at runtime.

Image 1

Sample of declarative security:

C#
[assembly : FileDialogPermissionAttribute(
            SecurityAction.RequestMinimum, 
            Unrestricted=true)]

Sample of imperative security:

C#
FileDialogPermission fileDlgPermission = new 
       FileDialogPermission(PermissionState.Unrestricted);
fileDlgPermission.Demand();

if (myDlgForm.ShowDialog() == DialogResult.OK)
    MessageBox.Show(myDlgForm.FileName);

.NET Framework Security Model

Image 2

Code Access Security (CAS)

Image 3

Permissions

Image 4

Code groups

Image 5

CLR examines all code groups in the hierarchy. When the code group is marked as Exclusive, then the CLR stops checking for group membership. Next, the CLR determines the permission sets for each code group. If the code is a member of the code group that is marked as Exclusive, only the permission set of that code group is taken into account, otherwise the CLR calculates the permission as a Union of all permission sets of all code groups that the running code is a member of.

Computing Permissions

Image 6

Any code group can have a LevelFinal property; in that case, the CLR stops its examinations there.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here