注销指定用户asp.net mvc [英] Logging out an specified user asp.net mvc
问题描述
如何从ASP.NET MVC 4应用程序中注销特定用户?我更喜欢使用User.Id属性来执行此操作.
How can I logout a specific user from my ASP.NET MVC 4 application? I prefer to perform this with the User.Id property.
推荐答案
基于您的评论,您的问题在于更改/更新/添加一个人的角色,但是您希望通过注销该人来体现这一点. 由于该添加/更改,新角色不会反映在用户的cookie中,而只会反映在数据库中.这就是他需要注销并再次登录才能进行此修改的原因.
Well, your problem, based on your comments, is about changing/updating/adding a person's role, but you wish to reflect this by logging him out. Because of that addition/change, the new role is not reflected into user's cookie, only in the database. That is the reason he needs to be log out and login again in order this modification to take place.
本质上,如果您正在使用cookie身份验证,那么在Startup.Auth.cs
中尝试该操作如何:
Essentially, if you are using cookie authentication, what about trying this in your 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将根据每个用户的请求进行更新.
Using the OnValidateIdentity will validate user's request every validateInterval
minutes, so in the code above the cookie will be updated every 1 minute. If you provide a TimeSpan.FromMinutes(0)
will mean that the cookie will be updated in each user's request.
也请查看有关StackOverflow的以下帖子和答案,以解决此特定问题.
Please check also the following posts and answers on StackOverflow in order to solve this particular issue.
- MVC 5 AddToRole是否需要注销才能生效?
- ASP.NET Identity,立即将另一个用户添加到角色中(不必注销并再次登录)
- 什么是ASP.NET Identity的IUserSecurityStampStore< TUser>界面?
- MVC 5 AddToRole requires logout before it works?
- ASP.NET Identity, add another user to role instantly (they don't have to log out and in again)
- What is ASP.NET Identity's IUserSecurityStampStore<TUser> interface?
希望这会有所帮助.
这篇关于注销指定用户asp.net mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!