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?
Here's my code:
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<ICompleteSamlLogin>();
var authenticationCookieContentMapper = httpContext.RequestServices.GetRequiredService<IAuthenticationCookieContentMapper>();
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;
};
