Ejemplo n.º 1
0
        protected override async Task HandleSignInAsync(SignInContext signin)
        {
            var result = await EnsureCookieTicket();

            var cookieOptions = BuildCookieOptions();

            var signInContext = new SidSigningInContext(Context, Options, Options.AuthenticationScheme, signin.Principal, new AuthenticationProperties(signin.Properties), cookieOptions);

            DateTimeOffset issuedUtc;

            if (signInContext.Properties.IssuedUtc.HasValue)
            {
                issuedUtc = signInContext.Properties.IssuedUtc.Value;
            }
            else
            {
                issuedUtc = Options.SystemClock.UtcNow;
                signInContext.Properties.IssuedUtc = issuedUtc;
            }

            if (!signInContext.Properties.ExpiresUtc.HasValue)
            {
                signInContext.Properties.ExpiresUtc = issuedUtc.Add(Options.ExpireTimeSpan);
            }

            await Options.Events.SigningIn(signInContext);

            if (signInContext.Properties.IsPersistent)
            {
                var expiresUtc = signInContext.Properties.ExpiresUtc ?? issuedUtc.Add(Options.ExpireTimeSpan);
                signInContext.CookieOptions.Expires = expiresUtc.ToUniversalTime();
            }

            var ticket = new AuthenticationTicket(signInContext.Principal, signInContext.Properties, signInContext.AuthenticationScheme);

            if (Options.SessionStore != null)
            {
                if (sessionKey != null)
                {
                    await Options.SessionStore.RemoveAsync(sessionKey);
                }
                sessionKey = await Options.SessionStore.StoreAsync(ticket);

                if (string.IsNullOrWhiteSpace(sessionKey))
                {
                    throw new ArgumentNullException(nameof(sessionKey));
                }
                var principal = new ClaimsPrincipal(
                    new ClaimsIdentity(new[] { new Claim(SessionIdClaim, sessionKey, ClaimValueTypes.String, Options.ClaimsIssuer) },
                                       Options.ClaimsIssuer));
                ticket = new AuthenticationTicket(principal, null, Options.AuthenticationScheme);
            }

            var cookieValue = Options.TicketDataFormat.Protect(ticket, GetTlsTokenBinding());

            Options.CookieManager.AppendResponseCookie(Context, Options.CookieName, cookieValue, signInContext.CookieOptions);

            var signedInContext = new SidSignedInContext(Context, Options, Options.AuthenticationScheme, signInContext.Principal, signInContext.Properties, null);

            await Options.Events.SignedIn(signedInContext);

            ApplyHeaders(signedInContext.Properties);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Implements the interface method by invoking the related delegate method.
 /// </summary>
 /// <param name="context"></param>
 public virtual Task SignedIn(SidSignedInContext context) => OnSignedIn(context);