如何使用ASP.NET Identity 2.0允许用户模拟另一个用户?

如何使用ASP.NET Identity 2.0允许用户模拟另一个用户?,第1张

如何使用ASP.NET Identity 2.0允许用户模拟另一个用户?

我已经找到了解决这个问题的方法。

基本上,我添加了具有管理员用户名的声明,如果该声明存在,我知道正在模拟。当管理员想要停止模拟时,系统会为声明检索原始用户名,删除旧的模拟cookie并为管理员创建新的cookie:

[AuthenticateAdmin] // <- make sure this endpoint is only available to adminspublic 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 Identity
2模拟用户

Asp.Net Core中的用户模拟

2017年7月更新:此主题非常受欢迎,因此我研究了Core中的用户模拟,并且更新的API的原理非常相似。模仿的方法如下:

    [Authorize(Roles = "Admin")] // <-- Make sure only admins can access this     public async Task<IActionResult> ImpersonateUser(String userId)    {        var currentUserId = User.GetUserId();        var impersonatedUser = await _userManager.FindByIdAsync(userId);        var userPrincipal = await _signInManager.CreateUserPrincipalAsync(impersonatedUser);        userPrincipal.Identities.First().AddClaim(new Claim("OriginalUserId", currentUserId));        userPrincipal.Identities.First().AddClaim(new Claim("IsImpersonating", "true"));        // sign out the current user        await _signInManager.SignOutAsync();        // If you use asp.net core 1.0        await HttpContext.Authentication.SignInAsync(cookieOptions.ApplicationcookieAuthenticationScheme, userPrincipal);        // If you use asp.net core 2.0 (the line above is deprecated)        await HttpContext.SignInAsync(cookieOptions.ApplicationcookieAuthenticationScheme, userPrincipal);        return RedirectToAction("Index", "Home");    }

这是停止模拟的方法:

    [Authorize(Roles = "Admin")] // <-- Make sure only admins can access this     public async Task<IActionResult> StopImpersonation()    {        if (!User.IsImpersonating())        { throw new Exception("You are not impersonating now. Can't stop impersonation");        }        var originalUserId = User.FindFirst("OriginalUserId").Value;        var originalUser = await _userManager.FindByIdAsync(originalUserId);        await _signInManager.SignOutAsync();        await _signInManager.SignInAsync(originalUser, isPersistent: true);        return RedirectToAction("Index", "Home");    }

我的博客中有完整说明:http : //tech.trailmax.info/2017/07/user-impersonation-in-asp-net-
core/

GitHub上的完整代码示例:https :
//github.com/trailmax/AspNetCoreImpersonation



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5011876.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存