11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: Building Secure ASP.NET Pages <strong>and</strong> Controls 287HttpContext context = HttpContext.Current;// Get the service provider from the contextIServiceProvider iServiceProvider = context as IServiceProvider;//Get a Type which represents an HttpContextType httpWorkerRequestType = typeof(HttpWorkerRequest);// Get the HttpWorkerRequest service from the service provider// NOTE: When trying to get a HttpWorkerRequest type from the HttpContext// unmanaged code permission is dem<strong>and</strong>ed.HttpWorkerRequest httpWorkerRequest =iServiceProvider.GetService(httpWorkerRequestType) as HttpWorkerRequest;// Get the token passed by IISIntPtr ptrUserToken = httpWorkerRequest.GetUserToken();// Create a WindowsIdentity from the tokenWindowsIdentity winIdentity = new WindowsIdentity(ptrUserToken);// Impersonate the userResponse.Write("Before impersonation: " +WindowsIdentity.GetCurrent().Name + "");WindowsImpersonationContext impContext = winIdentity.Impersonate();Response.Write("Impersonating: " + WindowsIdentity.GetCurrent().Name + "");// Place resource access code here// Stop impersonatingimpContext.Undo();Response.Write( "After Impersonating: " +WindowsIdentity.GetCurrent().Name + "");Note This approach assumes Forms or Passport authentication where your application’s virtualdirectory is configured in IIS to support anonymous access.If you use this code, use the following configuration:Note The code dem<strong>and</strong>s the unmanaged code permission<strong>Security</strong>Permission(<strong>Security</strong>PermissionFlag.UnmanagedCode), which is granted only to fully trusted<strong>Web</strong> applications.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!