用户 ASP.NET 模拟



我正在开发一个 ASP.Net 应用程序,我计划在其中实现应用程序管理员可以模拟用户并以该用户身份使用该应用程序的功能。如果有人对此有想法或经验,将非常有帮助。顺便说一句,我可以使用cookie来存储模拟用户数据并将其检索到应用程序中。

多谢。

更新我发现此解决方案 http://tech.trailmax.info/2014/06/user-impersonation-with-asp-net-identity-2/我的项目,但现在我面临其他一些问题。这也是我收到错误的代码示例。

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);
}

错误出在这条行var authenticationManager = context.GetOwinContext().Authentication;上。这里也是错误消息。

CS1061 C# 'HttpContext' 不包含 "GetOwinContext"和没有扩展方法"GetOwinContext"接受 可以找到类型为"HttpContext"的第一个参数(您是否缺少 使用指令或程序集引用?

任何想法我错过了什么。

GetOwinContext 扩展方法位于 System.Web.Http.Owin dll 中,需要作为 nuget 包下载(nuget 包名称为 Microsoft.AspNet.WebApi.Owin)

Install-Package Microsoft.AspNet.WebApi.Owin

https://learn.microsoft.com/en-us/nuget/guides/install-nuget