问题
I would like to change the logged in user to another user temporarily to do some process.
For example, say I am logged in as "Joe". In my method, I want to make the logged in user from "Joe" to "SuperUser", do some process, then change the logged in user back to "Joe". Can someone help with this?
回答1:
I think you want ASP.NET impersonation for that. Check out what it is and how to use it. Something like this (from the second link):
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo();
EDIT: For ASP.NET Membership, see this SO question and this answer.
回答2:
Although I fail to understand why a user becomes a superuser in a real world application, I think you could look at asp.net impersonation technique...
hth.
回答3:
From the answer to this question: Elevating process privilege programmatically?
You can indicate the new process should be started with elevated permissions by setting the Verb property of your startInfo object to 'runas', as follows:
startInfo.Verb = "runas";
This is changing your Windows user. If you want to change the ASP.NET user who logged in to your page, then this is not what you are looking for.
来源:https://stackoverflow.com/questions/2567573/how-do-i-change-the-logged-in-user-to-another