【问题标题】:Logging out an specific user in ASP.NET MVC在 ASP.NET MVC 中注销特定用户
【发布时间】:2021-09-26 23:54:00
【问题描述】:

如何从我的 ASP.NET MVC 4 应用程序中注销特定用户?我更喜欢使用 User.Id 属性来执行此操作。

【问题讨论】:

  • “指定用户”是指当前登录用户以外的用户吗?
  • @oj-raqueño 是的,例如,我是网站的管理员,我想注销网站的指定用户(成员)。
  • 好吧,如果您使用表单身份验证,您不能只是强迫他退出。他需要他的 cookie 过期。您可以做的是缩短 cookie 的生命周期并锁定该用户(如果您使用的是 ASP.NET Identity)。使用令牌的另一种方法是撤销他的令牌访问权限。您的身份验证流程是什么?
  • @gdyrrahitis 我使用 identity 。我的主要问题是,当我在应用程序中为用户赋予角色时,赋予该角色的用户需要注销并再次登录才能应用角色。

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

嗯,根据您的 cmets,您的问题是关于更改/更新/添加一个人的角色,但您希望通过注销他来反映这一点。 由于添加/更改,新角色不会反映到用户的 cookie 中,只会反映在数据库中。这就是他需要注销并再次登录才能进行此修改的原因。

基本上,如果您使用 cookie 身份验证,那么在您的 Startup.Auth.cs 中尝试这个怎么样:

app.UseCookieAuthentication(new CookieAuthenticationOptions {
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider {
        // Enables the application to validate the security stamp when the user logs in.
        // This is a security feature which is used when you change a password or add an external login to your account.  
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(1),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});

使用 OnValidateIdentity 将每validateInterval 分钟验证一次用户的请求,因此在上面的代码中,cookie 将每 1 分钟更新一次。如果您提供TimeSpan.FromMinutes(0) 将意味着cookie 将在每个用户的请求中更新。

请查看 StackOverflow 上的以下帖子和答案,以解决此特定问题。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-08
    • 2016-08-22
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2020-01-21
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多