2

I'm writing an ASP.net application and I'm working with the WindowsIdentity.GetCurrent() function. I want to know how (or if it's possible) to change what identity the ASP.net application runs as.

I eventually want to run it as an account associated with the end-user. I understand I can do this with Windows Authentication in IIS, but Windows Authentication will not work with my particular application. If I can programmatically log the user in with a AD username and password, that will be fine.

How can I set the WindowsIdentity the ASP.net application runs as without using Windows Authentication in IIS?

Update: This question has been sitting idle for a long time. I think that perhaps the framework does not allow me to do what I'm describing here. Does anyone know for sure that this is prohibited or considered bad practice?

Vivian River
  • 31,198
  • 62
  • 198
  • 313

1 Answers1

3

You need to use Impersonation

Or if you'd like to implement Impersonation using strictly Code, check this example:

How to implement impersonation in an ASP.NET application

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Impersonation works to a point to perform an action as a certain identity, however it is very cumbersome to have to impersonate with every action that must be performed as a certain user. With Windows Authentication, you can set in web.config and it will be impersonated automatically. I desire to have this same functionality even though I am not using impersonation. – Vivian River Jan 07 '10 at 18:14
  • You're not going to be able to accomplish what you're asking without using some form of impersonation. – Justin Niessner Jan 07 '10 at 18:17
  • So, to clarify then, are you saying that if I'm not using Windows Authentication in IIS, I cannot use the in the web.config to automatically impersonate a programmatically logged in user? – Vivian River Jan 07 '10 at 18:21
  • No, that's not what I'm saying. I thought you were saying that Impersonation wasn't acceptable for you. Check this table to see what is possible with the different settings: http://msdn.microsoft.com/en-us/library/aa302377.aspx – Justin Niessner Jan 07 '10 at 18:36
  • Actually, I've already seen the document you link to. In the table, the author indicates that without some Windows Authentication, only the ASP.net process or anonymous user will be returned. That's why I'm asking, is it possible to set the user returned by WindowsIdentity.GetCurrent() programmatically. – Vivian River Jan 07 '10 at 18:54
  • You could attempt to use something like the code found here: http://www.15seconds.com/Issue/040511.htm to map whatever your authentication scheme is to Windows Users...and then log those users on programatically. – Justin Niessner Jan 07 '10 at 19:02