Asp.net identity2, как я могу расширить скользящее поведение обновления идентичности

Используя asp.net identity2 с Google Auth..

Как я могу расширить его, чтобы я мог вызывать пользовательский код при обновлении метки времени slideExpiration?

using System;
using System.Configuration;
using System.Diagnostics;
using System.Security.Policy;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Helpers;
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)
        {
            Int64 cookieDurInMin = 10;
            // 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"),
                SlidingExpiration  = true,
                ExpireTimeSpan     = TimeSpan.FromMinutes( cookieDurInMin ),// How long to leave a "remember me" cookie valid - EWB
                CookieName         = "SP3GGS-ID2-cookie",
                //CookieSecure     = CookieSecureOption.Always, // TODO: turn this on for prod/qa so only ssl is allowed - EWB - per https://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/
                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.FromSeconds( 60 ),// how often to valdate against ad - EWB
                            regenerateIdentity: ( manager, user ) => user.GenerateUserIdentityAsync( manager )
                    ),
                    OnResponseSignIn = context =>
                    {
                        context.Properties.AllowRefresh = true;
                        context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes( cookieDurInMin );
                    },
                    ////OnApplyRedirect = context =>
                    ////{
                    ////    Trace.WriteLine( "OnApplyRedirect" );
                    ////},
                    //OnResponseSignedIn = context =>
                    //{
                    //    Trace.WriteLine( "OnResponseSignedIn" );
                    //},

                    //OnResponseSignOut = context =>
                    //{
                    //    Trace.WriteLine( "OnResponseSignOut" );
                    //}
                },
            } );            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);// HERE EWB

            // 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));

            // Enables the application to remember the second login verification factor such as phone or email.
            // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
            // This is similar to the RememberMe option when you log in.
            //app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

            // Uncomment the following lines to enable logging in with third party login providers
            //app.UseMicrosoftAccountAuthentication(
            //    clientId: "",
            //    clientSecret: "");

            //app.UseTwitterAuthentication(
            //   consumerKey: "",
            //   consumerSecret: "");

            //app.UseFacebookAuthentication(
            //   appId: "",
            //   appSecret: "");

            // https://coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/ setting breakpoint in OWIN login middlewear- EWB

            ///******************************************************************************************************************************
            ///*** TODO: WARNING HARD CODING THESE IS A BAD SECURITY RISK. MOVE TO APPSETTINGS, AND ENCRYPT IF REAL !!!!!!!!!!!!!!!!!!!!!!!
            ///******************************************************************************************************************************
            /// 
            // per https://docs.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 = "1032371756979-jtllvb3jdo2h2mg4ocr10o20i8il7r8s.apps.googleusercontent.com",
                ClientSecret = "VHUtLlxnB2Zfctp0QyCvu-9X"//,

                //SignInAsAuthenticationType = "ApplicationCookie"

                //ClientId = ConfigurationManager.AppSettings[ "aspnetidentitiy:ClientId" ],
                //ClientSecret = ConfigurationManager.AppSettings[ "aspnetidentitiy:ClientSecret" ]
            } );
            //});
        }
    }
}

0 ответов

Другие вопросы по тегам