Writing Secure Code In CSharp
2024-07-21 02:20:19
供稿:网友
writing secure code using csharpsubmitted byuser leveldate of submissionc.vinodh kumarintermediate04/06/2001
mobile code, which come from various sources like e-mail, documents and downloaded code over the internet are the main cause for the damage, destroy or copy private data. so to help protect computer systems from malicious mobile code and to provide a way to allow mobile code to run safely, the .net framework provides a security mechanism called code access security .
code access security is a mechanism that controls the access code has to protected resources and operations. net framework, code access security performs functions like defining permission, enables administrators to configure security policy, allows code to request the permissions it requires in order to run, grants permissions to each assembly that is loaded, based on the permissions requested by the code and enables code to demand that its callers have specific permissions.
code access security is a mechanism that grants/denies access to resources within a method call. for example, code written by a person may be allowed to write to the disk while code from another one may be forbidden from accessing the disk. this control can be enforced even if the code written by both of them is used within a single application.
system.security namespace provides the underlying structure of the .net framework security system, including interfaces, attributes, exceptions, and base classes for permissions and codeaccesspermission class defines the underlying structure of all code access permissions.
let see a sample application, which attempts to access a disk file and an environment variable .
code shown below will create permission to set read access to temp environment and full access to some files. before changing every file will be have a default permission set.
// create a permission set that allows read access to the temp
// environment variable and read, write, and append access to somefile from
//default permission
permissionset ps = new permissionset(permissionstate.none);
ps.addpermission(
new environmentpermission(environmentpermissionaccess.read, "temp"));
//adding various type of file level permission
ps.addpermission(
new fileiopermission(fileiopermissionaccess.read |
fileiopermissionaccess.write | fileiopermissionaccess.append,
"somefile"));
// make the permissions indicate all that we're allowed to do.
ps.assert();
permissionset class (in system.security) represents a collection and it contains many different kinds of permissions, and supports the methods that use and modify those permissions. we can add, remove, assert, deny and copy permission.
// deny access to the resources we specify
ps.deny();
// make the permissions indicate the only things that we're allowed to do.
ps.permitonly();
// remove the fileiopermissions from the permission set
ps.removepermission(typeof(fileiopermission))
// remove the environmentpermission from the permission set
ps.removepermission(typeof(environmentpermission));
deny method prevents callers from accessing the protected resource even if they have been granted permission to access it. pemitonly ensures that only the resources specified by this permission object can be accessed, even if the code has been granted permission to access other resources. fileiopermissionaccess specifies the actions that can be performed on the file or folder. environmentpermission class as the ability to query and modify system and user environment variables.
conclusion:we have seen how to write a secure code using the publicly available .net sdk. although what i have shown you is simple in functionality you can even create your own code access permission and much more advance security futures in you code.
source code: codesecureusingcsharp1.zip 2k