public static IAppBuilder ConfigureCookieAuthentication(this IAppBuilder app, CookieOptions options)
        {
            if (options == null) throw new ArgumentNullException("options");

            if (options.Prefix != null && options.Prefix.Length > 0)
            {
                options.Prefix += ".";
            }
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.PrimaryAuthenticationType,
                CookieName = options.Prefix + Constants.PrimaryAuthenticationType,
                ExpireTimeSpan = options.ExpireTimeSpan,
            });
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.ExternalAuthenticationType,
                CookieName = options.Prefix + Constants.ExternalAuthenticationType,
                AuthenticationMode = AuthenticationMode.Passive,
                ExpireTimeSpan = Constants.ExternalCookieTimeSpan
            });
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.PartialSignInAuthenticationType,
                CookieName = options.Prefix + Constants.PartialSignInAuthenticationType,
                AuthenticationMode = AuthenticationMode.Passive,
                ExpireTimeSpan = options.ExpireTimeSpan
            });
            return app;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AuthenticationOptions"/> class.
 /// </summary>
 public AuthenticationOptions()
 {
     EnableLocalLogin = true;
     EnableLoginHint = true;
     EnableSignOutPrompt = true;
     EnablePostSignOutAutoRedirect = false;
     PostSignOutAutoRedirectDelay = 0;
     RequireAuthenticatedUserForSignOutMessage = false;
     CookieOptions = new CookieOptions();
     SignInMessageThreshold = Constants.SignInMessageThreshold;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AuthenticationOptions"/> class.
 /// </summary>
 public AuthenticationOptions()
 {
     EnableLocalLogin = true;
     EnableSignOutPrompt = true;
     CookieOptions = new CookieOptions();
 }
        public static IAppBuilder ConfigureCookieAuthentication(this IAppBuilder app, CookieOptions options, IDataProtector dataProtector)
        {
            if (options == null) throw new ArgumentNullException("options");
            if (dataProtector == null) throw new ArgumentNullException("dataProtector");

            if (options.Prefix != null && options.Prefix.Length > 0)
            {
                options.Prefix += ".";
            }

            var primary = new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.PrimaryAuthenticationType,
                CookieName = options.Prefix + Constants.PrimaryAuthenticationType,
                ExpireTimeSpan = options.ExpireTimeSpan,
                SlidingExpiration = options.SlidingExpiration,
                TicketDataFormat = new TicketDataFormat(new DataProtectorAdapter(dataProtector, options.Prefix + Constants.PrimaryAuthenticationType))
            };
            app.UseCookieAuthentication(primary);

            var external = new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.ExternalAuthenticationType,
                CookieName = options.Prefix + Constants.ExternalAuthenticationType,
                AuthenticationMode = AuthenticationMode.Passive,
                ExpireTimeSpan = Constants.ExternalCookieTimeSpan,
                SlidingExpiration = false,
                TicketDataFormat = new TicketDataFormat(new DataProtectorAdapter(dataProtector, options.Prefix + Constants.ExternalAuthenticationType))
            };
            app.UseCookieAuthentication(external);

            var partial = new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.PartialSignInAuthenticationType,
                CookieName = options.Prefix + Constants.PartialSignInAuthenticationType,
                AuthenticationMode = AuthenticationMode.Passive,
                ExpireTimeSpan = options.ExpireTimeSpan,
                SlidingExpiration = options.SlidingExpiration,
                TicketDataFormat = new TicketDataFormat(new DataProtectorAdapter(dataProtector, options.Prefix + Constants.PartialSignInAuthenticationType))
            };
            app.UseCookieAuthentication(partial);

            Action<string> setCookiePath = path =>
            {
                if (!String.IsNullOrWhiteSpace(path))
                {
                    primary.CookiePath = external.CookiePath = path;
                    // TODO: should we leave the partial path to "/"?
                    partial.CookiePath = path;
                }
            };
            
            if (String.IsNullOrWhiteSpace(options.Path))
            {
                app.Use(async (ctx, next) =>
                {
                    // we only want this to run once, so assign to null once called 
                    // (and yes, it's possible that many callers hit this at same time, 
                    // but the set is idempotent)
                    if (setCookiePath != null)
                    {
                        setCookiePath(ctx.Request.PathBase.Value);
                        setCookiePath = null;
                    }
                    await next();
                });
            }
            else
            {
                setCookiePath(options.Path);
            }

            return app;
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AuthenticationOptions"/> class.
 /// </summary>
 public AuthenticationOptions()
 {
     EnableLocalLogin    = true;
     EnableSignOutPrompt = true;
     CookieOptions       = new CookieOptions();
 }
        public static IAppBuilder ConfigureCookieAuthentication(this IAppBuilder app, Thinktecture.IdentityServer.Core.Configuration.CookieOptions options, IDataProtector dataProtector)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (dataProtector == null)
            {
                throw new ArgumentNullException("dataProtector");
            }

            if (options.Prefix != null && options.Prefix.Length > 0)
            {
                options.Prefix += ".";
            }

            var primary = new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.PrimaryAuthenticationType,
                CookieName         = options.Prefix + Constants.PrimaryAuthenticationType,
                ExpireTimeSpan     = options.ExpireTimeSpan,
                SlidingExpiration  = options.SlidingExpiration,
                TicketDataFormat   = new TicketDataFormat(new DataProtectorAdapter(dataProtector, options.Prefix + Constants.PrimaryAuthenticationType))
            };

            app.UseCookieAuthentication(primary);

            var external = new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.ExternalAuthenticationType,
                CookieName         = options.Prefix + Constants.ExternalAuthenticationType,
                AuthenticationMode = AuthenticationMode.Passive,
                ExpireTimeSpan     = Constants.ExternalCookieTimeSpan,
                SlidingExpiration  = false,
                TicketDataFormat   = new TicketDataFormat(new DataProtectorAdapter(dataProtector, options.Prefix + Constants.ExternalAuthenticationType))
            };

            app.UseCookieAuthentication(external);

            var partial = new CookieAuthenticationOptions
            {
                AuthenticationType = Constants.PartialSignInAuthenticationType,
                CookieName         = options.Prefix + Constants.PartialSignInAuthenticationType,
                AuthenticationMode = AuthenticationMode.Passive,
                ExpireTimeSpan     = options.ExpireTimeSpan,
                SlidingExpiration  = options.SlidingExpiration,
                TicketDataFormat   = new TicketDataFormat(new DataProtectorAdapter(dataProtector, options.Prefix + Constants.PartialSignInAuthenticationType))
            };

            app.UseCookieAuthentication(partial);

            Action <string> setCookiePath = (path) =>
            {
                if (!String.IsNullOrWhiteSpace(path))
                {
                    primary.CookiePath = external.CookiePath = path;
                    // TODO: should we leave the partial path to "/"?
                    partial.CookiePath = path;
                }
            };

            if (String.IsNullOrWhiteSpace(options.Path))
            {
                app.Use(async(ctx, next) =>
                {
                    // we only want this to run once, so assign to null once called
                    // (and yes, it's possible that many callers hit this at same time,
                    // but the set is idempotent)
                    if (setCookiePath != null)
                    {
                        setCookiePath(ctx.Request.PathBase.Value);
                        setCookiePath = null;
                    }
                    await next();
                });
            }
            else
            {
                setCookiePath(options.Path);
            }

            return(app);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AuthenticationOptions"/> class.
 /// </summary>
 public AuthenticationOptions()
 {
     EnableLocalLogin = true;
     CookieOptions    = new CookieOptions();
 }