简体   繁体   English

如何将登录用户更改为其他用户?

[英]How do I change the logged in user to another?

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". 例如,假设我以“ 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". 在我的方法中,我要使已登录的用户从“ Joe”变为“ SuperUser”,进行一些处理,然后将已登录的用户更改回“ Joe”。 Can someone help with this? 有人可以帮忙吗?

I think you want ASP.NET impersonation for that. 我认为您希望对此进行ASP.NET模拟。 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 . 编辑:对于ASP.NET成员资格,请参阅此SO问题此答案

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... 尽管我无法理解为什么用户在现实应用程序中会成为超级用户,但我认为您可以看看asp.net模拟技术...

hth. hth。

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'来指示应使用提升的权限启动新进程,如下所示:

startInfo.Verb = "runas"; startInfo.Verb =“ runas”;

This is changing your Windows user. 这正在更改您的Windows用户。 If you want to change the ASP.NET user who logged in to your page, then this is not what you are looking for. 如果要更改登录到您的页面的ASP.NET用户,那么这不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM