[英]ASP.NET Identity sign out another user
Just like in this question , I want to sign out another user via updating the Security Stamp. 就像这个问题一样 ,我想通过更新“安全戳”来注销另一个用户。 However it doesn't work when testing on my local machine. 但是,在我的本地计算机上测试时,它不起作用。 I suspect the problem might be with the order of commands I'm using to reset a user and persisting the different properties to the db. 我怀疑问题可能与我用来重置用户并将不同属性持久保存到数据库的命令顺序有关。
That's my Startup.Auth
那就是我的Startup.Auth
public partial class Startup
{
public static TimeSpan expireTimeSpan = TimeSpan.FromHours(24);
public static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
ExpireTimeSpan = expireTimeSpan,
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
DataProtectionProvider = app.GetDataProtectionProvider();
}
}
And this is a controller method that allows changing another users email == username. 这是一种控制器方法,允许更改其他用户的电子邮件==用户名。 On changing the email, the user is supposed to be logged out and not have a valid password anymore. 更改电子邮件时,应该认为该用户已注销并且不再具有有效的密码。
public async Task<IHttpActionResult> UpdateUser(string id, ApplicationUser newUser)
{
var user = await _userManager.FindByIdAsync(id);
if (user == null) ...
IdentityResult result;
user.name = newUser.name;
user.roles = newUser.roles;
// sign out user and invalidate password
if (user.email != newUser.email)
{
user.email = newUser.email;
user.PasswordHash = null;
result = await _userManager.UpdateSecurityStampAsync(user.Id);
if (!result.Succeeded) throw new Exception("Security Stamp not updated.");
await _account.SendPasswordEmail(user);
}
result = await _userManager.UpdateAsync(user);
if (!result.Succeeded)
return GetErrorResult(result);
return Ok();
}
I have tried persisting the user first, then generating a new SecurityStamp
, but that didn't work either. 我尝试过先保留用户,然后再生成一个新的SecurityStamp
,但这也不起作用。
Any ideas what could be wrong? 任何想法可能有什么问题吗?
Thanks! 谢谢!
Apparently you didn't read the answer in the question you linked to clearly. 显然,您没有清楚地链接到问题中的答案。
// important to register UserManager creation delegate. Won't work without it
app.CreatePerOwinContext(UserManager.Create);
This is further explained here: 这在这里进一步说明:
https://aspnetidentity.codeplex.com/workitem/2209 https://aspnetidentity.codeplex.com/workitem/2209
However, you should be aware of this bug: 但是,您应该注意以下错误:
ExpireTimeSpan ignored after regenerateIdentity / validateInterval duration in MVC Identity (2.0.1) regenerateIdentity / validateInterval持续时间在MVC身份(2.0.1)中后被忽略ExpireTimeSpan
This will be fixed in the forthcoming Identity 2.2/Owin 3.0 release, but is not yet final. 这将在即将推出的Identity 2.2 / Owin 3.0版本中修复,但尚未最终确定。
https://aspnetidentity.codeplex.com/workitem/2347 https://aspnetidentity.codeplex.com/workitem/2347
Also see this blog article: 另请参阅此博客文章:
http://tech.trailmax.info/2014/08/cookieauthenticationprovider-and-user-session-invalidation-on-change-of-securitystamp/ http://tech.trailmax.info/2014/08/cookieauthenticationprovider-and-user-session-invalidation-on-change-of-securitystamp/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.