![]() |
|
![]() ![]()
![]() |
|
Secure ASP.
ASP.
Most of the time, a typical ASP.
But what if you need to access some protected resource,
such as a file on the local machine or a remote machine that is protected by
tight security, to which the ASP.
The other option, impersonation, is a bit trickier — but
far more powerful if used carefully. Impersonation in ASP.
There are several ways to implement impersonation, varying by the details used and the source of the impersonated credentials. Here I’ll take a brief look at configuring impersonation in web.config and one example of programmatic impersonation.
If you want to change the security context of all requests using the client/user’s security credentials, you can enable impersonation using the <identity> element in web.config:
<configuration> <system.web> <identity impersonate="true"> </system.web> </configuration>
You also can use the <location> element to make this setting more granular at the file or directory level.
If you want to specify a particular user to impersonate,
you can use the userName and Password attributes of the <identity>
element. If you leave off those attributes, ASP.
For more control, you can impersonate in code, such as to temporarily change security contexts before accessing a protected disk file. Here is a simple pattern you can use:
public void AccessProtectedResource() { WindowsIdentity identity = (WindowsIdentity)Context.User.Identity;
using (WindowsImpersonationContext wic = identity.Impersonate()) { // Do something with client credentials } }
As long as the client is authenticated, Context.User.Identity returns a WindowsIdentity object with the properties of the client’s security context. This object includes an Impersonate method that changes the security context and returns a WindowsImpersonationContext object.
It is critical to return the security context to its original state after accessing the protected resource by calling either the Undo or Dispose methods of the WindowsImpersonationContext object. In the code above, the call to Impersonate is in a using block, which calls the specified object’s Dispose method at the end of the block. This way you never have to worry about making the Undo or Dispose call yourself. If you fail to restore the original security context, the impersonated context will remain for the remainder of the page request, possibly leading to errors if it doesn’t have the necessary permissions for regular page creation operations.
There are plenty of other ways to impersonate
programmatically, such as to impersonate a particular Windows account other
than the current authenticated client. Explore the System.Security.Principal
namespace in the .
Impersonation can be a handy technique when you need to
access protected system resources from within an ASP.
Don
Kiely, MVP, MCSD, is a senior technology consultant, building custom
applications as well as providing business and technology consulting services.
His development work involves tools such as SQL Server, Visual Basic, C#, ASP.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||