tra*_*max 40 c# asp.net-mvc asp.net-identity
我正在将一个ASP.NET MVC 5.1应用程序从MembershipProvider迁移到ASP.NET Identity v2.0.我在应用程序中的一个功能是用户模拟:管理员可以在网站上注册的任何其他用户登录,而无需知道密码.
我使用此代码实现MembershipProvider的用户模拟,这不适用于Identity库.
如何在ASP.NET标识中实现用户模拟(而不是IIS模拟)?
tra*_*max 58
我找到了解决这个问题的方法.
基本上我用admin用户名添加声明,如果这个声明存在,我知道假冒正在发生.当管理员想要停止模拟时,系统会检索声明的原始用户名,删除旧的模拟cookie并为管理员创建新的Cookie:
[AuthenticateAdmin] // <- make sure this endpoint is only available to admins
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);
}
Run Code Online (Sandbox Code Playgroud)
更多信息在我的博客文章中:用户模仿ASP.Net Identity 2.
更新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");
}
Run Code Online (Sandbox Code Playgroud)
这是如何停止冒充:
[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");
}
Run Code Online (Sandbox Code Playgroud)
我的博客中有完整的解释:http://tech.trailmax.info/2017/07/user-impersonation-in-asp-net-core/ GitHub上的完整代码示例:https://github.com/trailmax/AspNetCoreImpersonation
| 归档时间: |
|
| 查看次数: |
9593 次 |
| 最近记录: |