如何使用ASP.NET身份2.0允许用户模拟其他用户? [英] How do I use ASP.NET Identity 2.0 to allow a user to impersonate another user?
问题描述
我从迁移到的MembershipProvider ASP.NET 2.0版的身份一个ASP.NET MVC 5.1应用程序。其中一个我在应用程序中的功能是用户模拟:管理员可以为在不知道密码的网站上注册的任何其他用户登录
I'm migrating a ASP.NET MVC 5.1 application from MembershipProvider to ASP.NET Identity v2.0. One of the features I have in the application is user impersonation: Administrators can be logged in as any other user registered on the site without knowing passwords.
我用这个code到<一个href=\"http://www.$c$cproject.com/Articles/43724/ASP-NET-Forms-authentication-user-impersonation\">implement为的MembershipProvider 用户模拟,这不符合身份库工作。
I used this code to implement user impersonation for the MembershipProvider and this does not work with Identity library.
如何在ASP.NET中实现身份用户模拟(不IIS模拟)?
How do I implement user impersonation (not IIS impersonation) in ASP.NET Identity?
推荐答案
我已经找到了解决这一问题。
I've found a solution to this problem.
基本上我补充索赔管理员的用户名,如果这种说法存在,我知道,模拟发生。当管理员想要停止模拟,系统检索索赔原来的用户名,删除旧模拟cookie的,并为管理员一个新的cookie:
Basically I add claim with admin username, if this claim exists, I know that impersonation is happening. When admin wants to stop impersonation, system retrieves original username for the claims, deletes old impersonated-cookie and creates a new cookie for the admin:
public async Task ImpersonateUserAsync(string userName)
{
var context = HttpContext.Current;
var originalUsername = context.User.Identity.Name;
var impersonatedUser = await userManager.FindByNameAsync(userName);
var impersonatedIdentity = await userManager.CreateIdentityAsync(impersonatedUser, DefaultAuthenticationTypes.ApplicationCookie);
impersonatedIdentity.AddClaim(new Claim("UserImpersonation", "true"));
impersonatedIdentity.AddClaim(new Claim("OriginalUsername", originalUsername));
var authenticationManager = context.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, impersonatedIdentity);
}
更多信息,在我的博客,帖子:用户使用假冒ASP.Net身份2 。
这篇关于如何使用ASP.NET身份2.0允许用户模拟其他用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!