Asp.net ID - Reset cookie and session on iis recycle (reload)

I have implemented asp.net mvc with asp.net id identification.

I used cookie based authentication. After restarting IIS / stop and starting IIS for my site, when I open my site, the user will automatically log in.

The user cookie is not cleared and remains valid for the user. How to get user to logout after iis reboot?

I used the default sample from the website. http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples

enter image description here

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(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });
+4
source share
2 answers

.

session asp.net ASP.NET MVC. strong > .

  • , .
  • , asp.net, .
  • , cookie .

    public class SessionHandler : ActionFilterAttribute
    {
        private ApplicationUserManager _userManager;
        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.Current.GetOwinContext().Authentication;
            }
        }
        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }
        public IIdentity UserIdentity
        {
            get { return System.Web.HttpContext.Current.User.Identity; }
        }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
    
            if (!string.IsNullOrWhiteSpace(UserIdentity.GetUserId()))
            {
                if (System.Web.HttpContext.Current.Session["Username"] == null)
                {
                    AuthenticationManager.SignOut();
                    filterContext.Result = new RedirectToRouteResult(
                                  new RouteValueDictionary
                                  {
                                       { "action", "Index" },
                                       { "controller", "Home" }
                                  });
                }
            }
        }
    }
    

Global.asax

GlobalFilters.Filters.Add(new SessionHandler());
0

cookie IIS - , HTTP. cookie IIS - IIS , , IIS - , ?

, cookie ApplicationUser.SecurityStamp . Startup.Auth.cs validateInterval: TimeSpan.FromMinutes(2) - cookie 2 SecurityStamp. - .

+1

Source: https://habr.com/ru/post/1655610/


All Articles