Пример #1
0
        public void ResponseSignedIn(CookieResponseSignedInContext context)
        {
            if (!context.Identity.HasClaim(c => c.Type == SPAddinClaimTypes.SPAddinAuthentication))
            {
                var shortHandUrl = context.Properties.RedirectUri.Replace("/", string.Empty);

                var navigationManager = new NavigationManager(context.OwinContext);

                var host = navigationManager.GetHostByShortHandUrl(shortHandUrl);
                if (host == null)
                {
                    throw new Exception("Unable to find host url");
                }

                var authRedirect = new UriBuilder(context.Request.Uri.GetLeftPart(UriPartial.Path))
                {
                    Path = shortHandUrl + "/Auth/AddinLogin"
                };
                var redirectUrl = WebUtilities.AddQueryString(authRedirect.ToString(), context.Options.ReturnUrlParameter, context.Properties.RedirectUri);
                redirectUrl = WebUtilities.AddQueryString(redirectUrl, "hostUrl", host.Url);

                context.Response.Redirect(redirectUrl);
            }
        }
Пример #2
0
 public override void ResponseSignedIn(CookieResponseSignedInContext context)
 {
     base.ResponseSignedIn(context);
 }
Пример #3
0
 /// <summary>
 /// Implements the interface method by invoking the related delegate method
 /// </summary>
 /// <param name="context"></param>
 public virtual void ResponseSignedIn(CookieResponseSignedInContext context)
 {
     OnResponseSignedIn.Invoke(context);
 }
Пример #4
0
        protected override async Task HandleSignInAsync(SignInContext signin)
        {
            var ticket = await EnsureCookieTicket();

            try
            {
                var tenantResolver = tenantResolverFactory.GetResolver();

                var cookieOptions = BuildCookieOptions();

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

                var signInContext = new CookieResponseSignInContext(
                    Context,
                    Options,
                    tenantResolver.ResolveAuthScheme(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);
                }

                Options.Notifications.ResponseSignIn(signInContext);

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

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

                    var principal = new ClaimsPrincipal(
                        new ClaimsIdentity(
                            new[] { new Claim(SessionIdClaim, _sessionKey, ClaimValueTypes.String, Options.ClaimsIssuer) },
                            Options.ClaimsIssuer));

                    //ticket = new AuthenticationTicket(principal, null, Options.AuthenticationScheme);
                    ticket = new AuthenticationTicket(principal, null, tenantResolver.ResolveAuthScheme(Options.AuthenticationScheme));
                }

                //var cookieValue = Options.TicketDataFormat.Protect(ticket);
                var ticketDataFormet = GetTicketDataFormat(tenantResolver);
                var cookieValue      = ticketDataFormet.Protect(ticket);

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

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

                //var signedInContext = new CookieResponseSignedInContext(
                //    Context,
                //    Options,
                //    Options.AuthenticationScheme,
                //    signInContext.Principal,
                //    signInContext.Properties);

                var signedInContext = new CookieResponseSignedInContext(
                    Context,
                    Options,
                    tenantResolver.ResolveAuthScheme(Options.AuthenticationScheme),
                    signInContext.Principal,
                    signInContext.Properties);

                Options.Notifications.ResponseSignedIn(signedInContext);

                var shouldLoginRedirect = Options.LoginPath.HasValue && OriginalPath == Options.LoginPath;
                ApplyHeaders(shouldLoginRedirect);
            }
            catch (Exception exception)
            {
                var exceptionContext = new CookieExceptionContext(Context, Options,
                                                                  CookieExceptionContext.ExceptionLocation.SignIn, exception, ticket);

                Options.Notifications.Exception(exceptionContext);
                if (exceptionContext.Rethrow)
                {
                    throw;
                }
            }
        }
Пример #5
0
 public void ResponseSignedIn(CookieResponseSignedInContext context)
 {
     //throw new NotImplementedException();
 }
Пример #6
0
 public void ResponseSignedIn(CookieResponseSignedInContext context)
 {
     context.OwinContext.TraceOutput.WriteLine($"ResponseSignedIn {context.AuthenticationType} {context.Identity.Name}");
 }
 public void ResponseSignedIn(CookieResponseSignedInContext context)
 {
 }
Пример #8
0
        /// <summary>
        /// Applies grants to the response
        /// </summary>
        /// <returns></returns>
        protected override async Task ApplyResponseGrantAsync()
        {
            AuthenticationResponseGrant signin = Helper.LookupSignIn(Options.AuthenticationType);
            bool shouldSignin = signin != null;
            AuthenticationResponseRevoke signout = Helper.LookupSignOut(Options.AuthenticationType, Options.AuthenticationMode);
            bool shouldSignout = signout != null;

            if (!(shouldSignin || shouldSignout || _shouldRenew))
            {
                return;
            }

            AuthenticationTicket model = await AuthenticateAsync();

            try
            {
                var cookieOptions = new CookieOptions
                {
                    Domain   = Options.CookieDomain,
                    HttpOnly = Options.CookieHttpOnly,
                    Path     = Options.CookiePath ?? "/",
                };
                if (Options.CookieSecure == CookieSecureOption.SameAsRequest)
                {
                    cookieOptions.Secure = Request.IsSecure;
                }
                else
                {
                    cookieOptions.Secure = Options.CookieSecure == CookieSecureOption.Always;
                }

                if (shouldSignin)
                {
                    var signInContext = new CookieResponseSignInContext(
                        Context,
                        Options,
                        Options.AuthenticationType,
                        signin.Identity,
                        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);
                    }

                    Options.Provider.ResponseSignIn(signInContext);

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

                    model = new AuthenticationTicket(signInContext.Identity, signInContext.Properties);
                    if (Options.SessionStore != null)
                    {
                        if (_sessionKey != null)
                        {
                            await Options.SessionStore.RemoveAsync(_sessionKey);
                        }
                        _sessionKey = await Options.SessionStore.StoreAsync(model);

                        ClaimsIdentity identity = new ClaimsIdentity(
                            new[] { new Claim(SessionIdClaim, _sessionKey) },
                            Options.AuthenticationType);
                        model = new AuthenticationTicket(identity, null);
                    }

                    string cookieValue = Options.TicketDataFormat.Protect(model);

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

                    var signedInContext = new CookieResponseSignedInContext(
                        Context,
                        Options,
                        Options.AuthenticationType,
                        signInContext.Identity,
                        signInContext.Properties);

                    Options.Provider.ResponseSignedIn(signedInContext);
                }
                else if (shouldSignout)
                {
                    if (Options.SessionStore != null && _sessionKey != null)
                    {
                        await Options.SessionStore.RemoveAsync(_sessionKey);
                    }

                    var context = new CookieResponseSignOutContext(
                        Context,
                        Options,
                        cookieOptions);

                    Options.Provider.ResponseSignOut(context);

                    Options.CookieManager.DeleteCookie(
                        Context,
                        Options.CookieName,
                        context.CookieOptions);
                }
                else if (_shouldRenew && model.Identity != null)
                {
                    model.Properties.IssuedUtc  = _renewIssuedUtc;
                    model.Properties.ExpiresUtc = _renewExpiresUtc;

                    if (Options.SessionStore != null && _sessionKey != null)
                    {
                        await Options.SessionStore.RenewAsync(_sessionKey, model);

                        ClaimsIdentity identity = new ClaimsIdentity(
                            new[] { new Claim(SessionIdClaim, _sessionKey) },
                            Options.AuthenticationType);
                        model = new AuthenticationTicket(identity, null);
                    }

                    string cookieValue = Options.TicketDataFormat.Protect(model);

                    if (model.Properties.IsPersistent)
                    {
                        cookieOptions.Expires = _renewExpiresUtc.ToUniversalTime().DateTime;
                    }

                    Options.CookieManager.AppendResponseCookie(
                        Context,
                        Options.CookieName,
                        cookieValue,
                        cookieOptions);
                }

                Response.Headers.Set(
                    HeaderNameCacheControl,
                    HeaderValueNoCache);

                Response.Headers.Set(
                    HeaderNamePragma,
                    HeaderValueNoCache);

                Response.Headers.Set(
                    HeaderNameExpires,
                    HeaderValueMinusOne);

                bool shouldLoginRedirect  = shouldSignin && Options.LoginPath.HasValue && Request.Path == Options.LoginPath;
                bool shouldLogoutRedirect = shouldSignout && Options.LogoutPath.HasValue && Request.Path == Options.LogoutPath;

                if ((shouldLoginRedirect || shouldLogoutRedirect) && Response.StatusCode == 200)
                {
                    IReadableStringCollection query = Request.Query;
                    string redirectUri = query.Get(Options.ReturnUrlParameter);
                    if (!string.IsNullOrWhiteSpace(redirectUri) &&
                        IsHostRelative(redirectUri))
                    {
                        var redirectContext = new CookieApplyRedirectContext(Context, Options, redirectUri);
                        Options.Provider.ApplyRedirect(redirectContext);
                    }
                }
            }
            catch (Exception exception)
            {
                CookieExceptionContext exceptionContext = new CookieExceptionContext(Context, Options,
                                                                                     CookieExceptionContext.ExceptionLocation.ApplyResponseGrant, exception, model);
                Options.Provider.Exception(exceptionContext);
                if (exceptionContext.Rethrow)
                {
                    throw;
                }
            }
        }
Пример #9
0
 protected virtual void DoResponseSignedIn(CookieResponseSignedInContext context)
 {
     var s = context.Response.StatusCode;
 }
 public void ResponseSignedIn(CookieResponseSignedInContext context)
 {
 }
 public override void ResponseSignedIn(CookieResponseSignedInContext context)
 {
     context.Options.CookiePath = TenantCookiePath();
     base.ResponseSignedIn(context);
 }