Sustainsys Saml2 HandledResult = true still sets the cookie
09:22 06 Nov 2025

On AcsCommandResultCreated, I want to set my custom cookie. However, I end up with two cookies: one created by the library and another that is mine, even though I set HandledResult = true. How can I have only my authentication cookie?

enter image description here

Here's my code

            options.SPOptions.EntityId = new EntityId(samlAuthenticationConfig.EntityId);
            options.SPOptions.ModulePath = samlRoutePrefix;
            options.SignInScheme = "SamlTemp";

           options.Notifications.AcsCommandResultCreated = (commandResult, _) =>
            {
                var httpContext = httpContextAccessor.HttpContext;
                if (httpContext == null)
                    throw new InvalidOperationException("HttpContext not available — ensure IHttpContextAccessor is registered.");

                var completeSamlLogin = httpContext.RequestServices.GetRequiredService();
                var authenticationCookieContentMapper = httpContext.RequestServices.GetRequiredService();

                var relayState = httpContext.Request.Form[SamlConstants.RelayStateKey].ToString();

                var claimsPrincipal = commandResult.Principal;

                var email = ExtractEmailFromClaims(claimsPrincipal);
                if (string.IsNullOrEmpty(email))
                    throw new UnauthenticatedException("No email address found in SAML response.");

                var firstName = claimsPrincipal.FindFirst(ClaimTypes.GivenName)?.Value;
                var lastName = claimsPrincipal.FindFirst(ClaimTypes.Surname)?.Value;

                var input = new CompleteSamlLoginInput
                {
                    RelayState = relayState,
                    Email = email,
                    FirstName = firstName,
                    LastName = lastName
                };

                var loginOutput = completeSamlLogin.Execute(input).Result;
                var cookieContent = authenticationCookieContentMapper.Map(loginOutput);

                httpContext.CreateAuthenticationCookieAsync(cookieContent, true).GetAwaiter().GetResult();

                commandResult.Headers.Add(SCloudHeaderNames.AntiCsrfToken, cookieContent.AntiCsrfToken.ToString());

                commandResult.HandledResult = true;
            };
.net single-sign-on saml sustainsys-saml2