In most cases, designing a website will require some form of user validation. Some users will be granted more permissions than others, which complicates the process of testing and debugging. One method to ease this process is to impersonate a user of a different permission.
If users with access to the website can be trusted, perhaps a company intranet or home network, this easy method of user impersonation includes a value key combination in the WebConfig file (to read an article on Store Constant Variables in WebConfig). The high level idea is to create two keys to control user login. One is of the debugger’s username (yourAccount) and the second of the username you want to impersonate (accountToImpersonate).
<appSettings>
<add key = “currentDebugger” value = “yourAccount” />
<add key = “debuggerImpersonateAs” value = “accountToImpersonate” />
</appSettings>
With these appSettings, you want to create a function to manipulate the username before it is evaluated for permission levels. The function will switch the login username to accountToImpersonate whenever you log in as yourAccount allowing you specifiec access. Additionally, because the changes occur in the WebConfig file, changes can be made to the accountToImpersonate account without requiring a recompile.
public static string currentUser(string username)
{
if (username== ConfigurationSettings.AppSettings["currentDebugger"])
{
username = ConfigurationSettings.AppSettings["debuggerImpersonateAs"];
}
return username;
}
The function above will return either the username that is logged in or if you are the currentDebugger will return debuggerImpersonateAs. When the debug process is over, simply leave the currentDebugger value blank as shown below and you can log in as usual to yourAccount.
<appSettings>
<add key = “currentDebugger” value = “” />
<add key = “debuggerImpersonateAs” value = “accountToImpersonate” />
</appSettings>
An important security issue to keep in mind is that this method should only be used on website located on a trusted network. The important settings are saved in the WebConfig file, which can be altered without requiring a recompile, and therefore presents a potential security risk on open networks or the internet.




