Click here to Skip to main content
15,888,053 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
We have web apps calling the Impsersoate Class represented in this article:
A small C# Class for impersonating a User[^]

The class is currently called in Page_Load. Is there a way to call this class one time to set impersonation for the entire session rather than on each Page_Load? I tried moving the call to Session_Start - but doesn't seem to work there. Suggestions or articles are appreciated. thanks
Posted

1 solution

The class you used is for when you only want a piece of your code to be impersonated. If you always want to impersonate another user you don't even need to write code, there is a web.config setting for that.

Here is a link to a .net 2003 article (first one that came up in search) but it still applies:
http://msdn.microsoft.com/en-us/library/aa292118(v=vs.71).aspx[^]

XML
<identity impersonate="true">
          userName="domain\user" 
          password="password" />
</identity>
 
Share this answer
 
Comments
Member 9428144 20-May-14 12:02pm    
Yes thanks. I knew about this identity tag and I should have posted my entire scenario (sorry).
We are also avoiding having hardcoded passwords visible. We have a third-party application we call to get the password of our application ID. Once I have that password - I then call the Impersonate Class with the username and password. This way we avoid having that password visible in web.config.
I also know I can have an encrypted file reference in web.config which does conceal the password. But again - the third party application has the ability to change an account password on the platform - so we also want to avoid having to regenerate encrypted config files. Having a contained class/dll we can call is a great solution to all of this. But I want to avoid calling it every time the app hits Page_Load. Again - sorry my original question did not include all this context.
ZurdoDev 20-May-14 12:16pm    
No problem. That makes sense. In that case the solution you found is the only option I can think of. However, you can simplify it by creating a class that inherits System.Web.UI.Page and call it BasePage or something and then all of your pages inherit from that. Then in BasePage OnInit or Page_Load you call it once and all pages will get it.
Member 9428144 20-May-14 12:50pm    
Well - I already have a page like that but I didn't think to put it there. I'll give it a try and Accept the solution if it works. thanks
ZurdoDev 20-May-14 12:54pm    
OK.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900