繁体   English   中英

ASP.NET身份注销另一个用户

[英]ASP.NET Identity sign out another user

就像这个问题一样 ,我想通过更新“安全戳”来注销另一个用户。 但是,在我的本地计算机上测试时,它不起作用。 我怀疑问题可能与我用来重置用户并将不同属性持久保存到数据库的命令顺序有关。

那就是我的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();
    }
}

这是一种控制器方法,允许更改其他用户的电子邮件==用户名。 更改电子邮件时,应该认为该用户已注销并且不再具有有效的密码。

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

我尝试过先保留用户,然后再生成一个新的SecurityStamp ,但这也不起作用。

任何想法可能有什么问题吗?

谢谢!

显然,您没有清楚地链接到问题中的答案。

// important to register UserManager creation delegate. Won't work without it
app.CreatePerOwinContext(UserManager.Create);

这在这里进一步说明:

https://aspnetidentity.codeplex.com/workitem/2209

但是,您应该注意以下错误:

regenerateIdentity / validateInterval持续时间在MVC身份(2.0.1)中后被忽略ExpireTimeSpan

这将在即将推出的Identity 2.2 / Owin 3.0版本中修复,但尚未最终确定。

https://aspnetidentity.codeplex.com/workitem/2347

另请参阅此博客文章:

http://tech.trailmax.info/2014/08/cookieauthenticationprovider-and-user-session-invalidation-on-change-of-securitystamp/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM