简体   繁体   English

ASP.NET身份-更新用户记录后保持登录

[英]ASP.NET Identity - Maintain login after updating user record

In a ASP.NET MVC web application, the admin may sometimes have the need to modify his user profile, thus altering his DB AspNetUsers record and triggering a SecurityStamp regeneration. 在ASP.NET MVC Web应用程序中,管理员有时可能需要修改其用户配置文件,从而更改其DB AspNetUsers记录并触发SecurityStamp重新生成。

Modifying the SecurityStamp will eventually trigger the identity validation every 30 minutes server-side and cut the user's authentication, sending him back to login. 修改SecurityStamp最终将在服务器端每30分钟触发一次身份验证,并削减用户的身份验证,将其发送回登录。

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            ExpireTimeSpan = TimeSpan.FromMinutes(30), 
            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(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });         

Is there a way to prevent this from happening but allowing me to keep the validation active? 有什么办法可以防止这种情况的发生,但可以让我保持激活状态吗? (something like forcing an identity "realignment" between server and client when saving the changes on the user profile) (类似于在用户配置文件上保存更改时强制服务器和客户端之间进行身份“重新对齐”)

Thanks in advance for every advice! 预先感谢您的每一个建议! - Gigi -吉吉

You can re-SignIn the User after the changes have been made (this code assumes you have UserManager and SignInManager available as per the default Account controller and an async ActionResult): 您可以在进行更改后重新登录用户(此代码假定您具有默认帐户控制器和异步ActionResult可用的UserManager和SignInManager):

        ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
        if (user != null)
        {
            await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
        }

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

相关问题 登录后的ASP.NET身份用户为null - ASP.NET Identity user null after login asp.net身份多用户登录 - asp.net identity multipe user login 用户登录后,ASP.net loginView控件不会更新 - ASP.net loginView control does not updating after user login ASP.NET身份-如何维护登录用户角色? - Asp.net identity - How to maintain logged in user role? 建立用户角色之前登录后的ASP.NET Identity重定向 - ASP.NET Identity redirecting after login before user roles are established 用户登录后将自定义属性添加到ASP.NET MVC身份 - Add a Custom property to ASP.NET MVC Identity after user login 在ASP.net Identity中,如何在登录后将用户重定向到特定的URL? - In ASP.net Identity, how do you redirect a user to a specific url after login? 在ASP.NET Core上使用Identity登录后,如何直接获取用户详细信息? - How do I get the user details straight after login using Identity on ASP.NET Core? 在 asp.net 核心成功登录后,User.Identity.IsAuthenticated 在非身份验证方法中为 false - User.Identity.IsAuthenticated is false in a non-auth methods after successful login in asp.net core 登录成功后传递用户详细信息来自身份asp.net core 3.1 - Pass user detail after Login Is successful From identity asp.net core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM