简体   繁体   English

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

[英]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.

相关问题 ASP.net Identity 2.0退出另一个用户 - ASP.net Identity 2.0 Sign-out another user 如何在 ASP.NET Core Identity 中注销其他用户 - How to sign out other user in ASP.NET Core Identity 如何在ASP.NET Core Identity中注销用户 - How to sign out a user in ASP.NET Core Identity ASP.Net Identity注销所有会话 - ASP.Net Identity sign-out all sessions 成功登录后,ASP.NET Identity无法验证用户身份 - ASP.NET Identity does not authenticate user after successful sign in ASP.NET身份,立即将另一个用户添加到角色(他们不必再次注销) - ASP.NET Identity, add another user to role instantly (they don't have to log out and in again) 有没有办法使用 Windows 身份验证注销并以其他用户身份登录? [ASP.NET Core 3.1 MVC] - Is there a way to sign out and login as another user using Windows authentication? [ASP.NET Core 3.1 MVC] 为什么我的asp.net身份-user会自动注销 - Why my asp.net identity -user will log out automatically ASP.NET Identity 2 记住我 - 用户正在注销 - ASP.NET Identity 2 Remember Me - User Is Being Logged Out 使用ASP.NET Core身份验证和基于令牌的身份验证时,为什么要登录新用户? - Why sign-in a new user when using ASP.NET Core Identity and token-based auth?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM