protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            RequestCookieCollection cookies = Request.Cookies;
            string cookie = cookies[Options.CookieName];

            if (string.IsNullOrWhiteSpace(cookie))
            {
                return(null);
            }

            AuthenticationTicket ticket = Options.TicketDataFormat.Unprotect(cookie);

            if (ticket == null)
            {
                _logger.WriteWarning(@"Unprotect ticket failed");
                return(null);
            }

            DateTimeOffset currentUtc = Options.SystemClock.UtcNow;
            DateTimeOffset?issuedUtc  = ticket.Properties.IssuedUtc;
            DateTimeOffset?expiresUtc = ticket.Properties.ExpiresUtc;

            if (expiresUtc != null && expiresUtc.Value < currentUtc)
            {
                return(null);
            }

            if (issuedUtc != null && expiresUtc != null && Options.SlidingExpiration)
            {
                TimeSpan timeElapsed   = currentUtc.Subtract(issuedUtc.Value);
                TimeSpan timeRemaining = expiresUtc.Value.Subtract(currentUtc);

                if (timeRemaining < timeElapsed)
                {
                    _shouldRenew    = true;
                    _renewIssuedUtc = currentUtc;
                    TimeSpan timeSpan = expiresUtc.Value.Subtract(issuedUtc.Value);
                    _renewExpiresUtc = currentUtc.Add(timeSpan);
                }
            }

            var context = new CookieValidateIdentityContext(Context, ticket, Options);

            await Options.Provider.ValidateIdentity(context);

            return(new AuthenticationTicket(context.Identity, context.Properties));
        }
Example #2
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationTicket ticket = null;

            try
            {
                string cookie = Options.CookieManager.GetRequestCookie(Context, Options.CookieName);
                if (string.IsNullOrWhiteSpace(cookie))
                {
                    return(null);
                }

                ticket = Options.TicketDataFormat.Unprotect(cookie);

                if (ticket == null)
                {
                    _logger.WriteWarning(@"Unprotect ticket failed");
                    return(null);
                }

                if (Options.SessionStore != null)
                {
                    Claim claim = ticket.Identity.Claims.FirstOrDefault(c => c.Type.Equals(SessionIdClaim));
                    if (claim == null)
                    {
                        _logger.WriteWarning(@"SessoinId missing");
                        return(null);
                    }
                    _sessionKey = claim.Value;
                    ticket      = await Options.SessionStore.RetrieveAsync(_sessionKey);

                    if (ticket == null)
                    {
                        _logger.WriteWarning(@"Identity missing in session store");
                        return(null);
                    }
                }

                DateTimeOffset currentUtc = Options.SystemClock.UtcNow;
                DateTimeOffset?issuedUtc  = ticket.Properties.IssuedUtc;
                DateTimeOffset?expiresUtc = ticket.Properties.ExpiresUtc;

                if (expiresUtc != null && expiresUtc.Value < currentUtc)
                {
                    if (Options.SessionStore != null)
                    {
                        await Options.SessionStore.RemoveAsync(_sessionKey);
                    }
                    return(null);
                }

                bool?allowRefresh = ticket.Properties.AllowRefresh;
                if (issuedUtc != null && expiresUtc != null && Options.SlidingExpiration &&
                    (!allowRefresh.HasValue || allowRefresh.Value))
                {
                    TimeSpan timeElapsed   = currentUtc.Subtract(issuedUtc.Value);
                    TimeSpan timeRemaining = expiresUtc.Value.Subtract(currentUtc);

                    if (timeRemaining < timeElapsed)
                    {
                        _shouldRenew    = true;
                        _renewIssuedUtc = currentUtc;
                        TimeSpan timeSpan = expiresUtc.Value.Subtract(issuedUtc.Value);
                        _renewExpiresUtc = currentUtc.Add(timeSpan);
                    }
                }

                var context = new CookieValidateIdentityContext(Context, ticket, Options);

                await Options.Provider.ValidateIdentity(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception exception)
            {
                CookieExceptionContext exceptionContext = new CookieExceptionContext(Context, Options,
                                                                                     CookieExceptionContext.ExceptionLocation.AuthenticateAsync, exception, ticket);
                Options.Provider.Exception(exceptionContext);
                if (exceptionContext.Rethrow)
                {
                    throw;
                }
                return(exceptionContext.Ticket);
            }
        }
 /// <summary>
 /// Implements the interface method by invoking the related delegate method
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public virtual Task ValidateIdentity(CookieValidateIdentityContext context)
 {
     return(OnValidateIdentity.Invoke(context));
 }