views:

551

answers:

2

I believe that I have successfully impersonated my own user account while running an ASP.NET page on my local machine.

Using the method described here, I have successfully changed the WindowsIdentity.GetCurrent().Name from ASPNET to my domain account.

I can successfully write to a file on the file system that ONLY my account has permission to access. However when I try to delete a Performance Counter Category, I get Access Denied.
I have auditing on the branch of the registry and its telling me that MyMachine\ASPNET is Failing at Object Access.

Here is the code it is failing on:

if ( PerformanceCounterCategory.Exists ( PerfmonCategory ) )
       PerformanceCounterCategory.Delete ( PerfmonCategory );

Its failing on the Delete Call.

(My account is admin and I can run the same code outside an ASP.NET context successfully).

I suspect that this System.Diagnostics namespace call is actually calling some COM process and somehow I am being bounced because of a 2nd hop. Can anyone confirm what might be going on?

Edit: The Exception: Access is denied Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ComponentModel.Win32Exception: Access is denied

Running under full trust.

+1  A: 

You do indeed need to be an admin in order to add or remove performance counters.

I'm not sure why you'd want to use Win32 API calls to do your impersonation - it's been a while since I've messed with it, but I think all you need to do is use

WindowsIdentity.GetCurrent().Impersonate()

To be clear, you'll first need to authenticate in your web application using Windows authentication, and then you should be able to make the call to Impersonate().

Impersonate() Method

Daniel Schaffer
Actually you still need all them lovely Win32 API calls to get the correct logon token: WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken) http://msdn.microsoft.com/en-us/library/chf6fbt4.aspx
Kev
I don't think you *need* the logonToken... that's why there's a parameterless overload.
Daniel Schaffer
I've actually impersonated both ways with no change in effect.
Jeff Martin
I know that the impersonation is working as I can write to the file with restricted permissions.
Jeff Martin
+1  A: 

You could run your application on its own application pool (always a good thing) and assign it a service user the appropriate rights, that way you don't need to mess with impersonation.

Otávio Décio
Giving an app pool admin rights is a bit scary, don't you think? Besides, I doubt it is going to need those rights *all* the time, so it is better to elevate only when needed.
Daniel Schaffer
Dumb question - why are you creating / deleting *categories* cfrom inside your web app? I thought you would be creating *instances* of counters.
Otávio Décio
because in this case, I am running in a local dev environment and the performance counters are configured in the web.config. When the app is intalled to shared dev, QA, or Production, the installer takes care of the counter creation. I wanted to make it easy in the local dev environment to use.
Jeff Martin
Well then giving the app pool admin rights doesn't look that scary - it's just dev, so maybe there is little harm...
Otávio Décio