示例#1
0
        private async Task <bool> HandleSignIn()
        {
            if (!Request.Path.Value.EndsWith(Options.AssertionConsumerServiceUrl, StringComparison.OrdinalIgnoreCase) ||
                !_httpRedirectBinding.IsValid(Context.Request))
            {
                return(false);
            }

            _logger.LogDebug($"Entering {nameof(HandleSignIn)}");

            var properties = await _sessionStore.LoadAsync <AuthenticationProperties>() ?? new AuthenticationProperties();

            properties.Items.TryGetValue(AuthnRequestIdKey, out var initialAuthnRequestId);

            var result = _httpRedirectBinding.GetResponse(Context.Request);
            var base64EncodedSamlResponse = result.Response;
            var assertion = _samlService.HandleHttpRedirectResponse(base64EncodedSamlResponse, initialAuthnRequestId);

            await SignIn(assertion, properties);

            await _sessionStore.RemoveAsync <AuthenticationProperties>();

            var redirectUrl = GetRedirectUrl(properties);

            _logger.LogDebug(
                $"Method={nameof(HandleSignIn)}. Received and handled SSO redirect response. Redirecting to {redirectUrl}");

            Context.Response.Redirect(redirectUrl, true);
            return(true);
        }
        private async Task <bool> HandleSignIn()
        {
            if (!Request.Path.Value.EndsWith(Options.AssertionConsumerServiceUrl, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            _logger.LogDebug($"Entering {nameof(HandleSignIn)}");

            if (!_httpRedirectBinding.IsValid(Context.Request))
            {
                return(false);
            }

            var initialAuthnRequestId = GetRequestId();
            var result = _httpRedirectBinding.GetResponse(Context.Request);
            var base64EncodedSamlResponse = result.Response;
            var assertion = _samlService.HandleHttpRedirectResponse(base64EncodedSamlResponse, initialAuthnRequestId);

            var authenticationProperties =
                Options.StateDataFormat.Unprotect(result.RelayState) ?? new AuthenticationProperties();

            await SignIn(assertion, authenticationProperties);

            var cookieOptions = Options.RequestIdCookie.Build(Context, Clock.UtcNow);

            Response.DeleteAllRequestIdCookies(Context.Request, cookieOptions);

            var redirectUrl = GetRedirectUrl(authenticationProperties);

            _logger.LogDebug(
                $"Method={nameof(HandleSignIn)}. Received and handled SSO redirect response. Redirecting to {redirectUrl}");

            Context.Response.Redirect(redirectUrl, true);
            return(true);
        }
示例#3
0
        private async Task <bool> HandleSignIn()
        {
            if (!Request.Path.Value.EndsWith(Options.AssertionConsumerServiceUrl, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            _logger.LogDebug($"Entering {nameof(HandleSignIn)}");

            if (!_httpRedirectBinding.IsValid(Context.Request))
            {
                return(false);
            }

            var initialAuthnRequestId = GetRequestId();
            var result = _httpRedirectBinding.GetResponse(Context.Request);
            var base64EncodedSamlResponse = result.Response;

            try
            {
                var assertion =
                    _samlService.HandleHttpRedirectResponse(base64EncodedSamlResponse, initialAuthnRequestId);

                var authenticationProperties =
                    Options.StateDataFormat.Unprotect(result.RelayState) ?? new AuthenticationProperties();
                await SignIn(assertion, authenticationProperties);

                var cookieOptions = Options.RequestIdCookie.Build(Context, Clock.UtcNow);
                Response.DeleteAllRequestIdCookies(Context.Request, cookieOptions);

                var redirectUrl = GetRedirectUrl(authenticationProperties);

                var isPassive = false;
                foreach (var item in authenticationProperties.Items)
                {
                    if (item.Key == "IsPassive")
                    {
                        isPassive = item.Value == "true";
                    }
                }

                _logger.LogDebug(
                    $"Method={nameof(HandleSignIn)}. Received and handled SSO redirect response. Redirecting to {redirectUrl}");

                if (isPassive)
                {
                    Context.Response.Redirect(redirectUrl + "?passive=true", true);
                }
                else
                {
                    Context.Response.Redirect(redirectUrl, true);
                }
            }
            catch (Saml2Exception e)
            {
                if (e.Message.Contains("NoPassive"))
                {
                    var authenticationProperties =
                        Options.StateDataFormat.Unprotect(result.RelayState) ?? new AuthenticationProperties();
                    var redirectUrl = GetRedirectUrl(authenticationProperties) + "?passive=false";
                    Context.Response.Redirect(redirectUrl, true);
                    return(true);
                }
            }

            return(true);
        }