Search code examples
asp.netasp.net-identityasp.net-identity-2google-signin

Created a mvc5 app with Identity2, how do i set it up to use session cookies, so they expire when the browser closes


Created a mvc5 app with Identity2,using google login (pretty much the empty app, with google stuff turned on)

How do I set it up to use session cookies, so they expire when the browser closes. The app will be used by students who may hot swap seats, so i need the login to expire when the browser closes.

I read an SO article that implies this is the default, but when i close the browser, and go back to the site, it remembers the google login.

Edit

Sorry to burst everyone bubble, but this isn't a duplicate.

It reproduced in Chrome after the settings in the supposed "answer" are changed, and it also reproduces in IE... This is an Asp.net Identity 2 +Google login issue, not a Chrome issue.

Edit

Adding Startup Auth file for Setup Help

using System;
using System.Configuration;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Owin;
using StudentPortalGSuite.Models;

namespace StudentPortalGSuite
{
    public partial class Startup
    {
        // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            // 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"),
                               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 )
                        )
                }, 
            });            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

            // per https://learn.microsoft.com/en-us/aspnet/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on - EWB
            //dev-jcsn email
            app.UseGoogleAuthentication( new GoogleOAuth2AuthenticationOptions()
            {
                ClientId     = "...",
                ClientSecret = "..."


            } );
            //});
        }
    }
}

EDIT The use case I'm trying to fix is, since our app is used in a classroom, that student A Closes his/her browser instead of logging out, and then next user tries to login. As it stands they are autologged into user A's account.

I'd also be up for a way to 100% log out the user when redirected to the login page, but all the ways I've tried that aren't working.


Solution

  • Calling this at the top of the LogIn controller Method solved the issue.

      Request.GetOwinContext().Authentication.SignOut( DefaultAuthenticationTypes.ApplicationCookie );// https://stackoverflow.com/questions/28999318/owin-authentication-signout-doesnt-seem-to-remove-the-cookie - stralos s answer
      Request.GetOwinContext().Authentication.SignOut( DefaultAuthenticationTypes.ExternalCookie );