Saml No Cookie Preserving State ASP.NET CORE
Aug 20 2020
var certbase = _env.IsDevelopment() ? "" : AppDomain.CurrentDomain.BaseDirectory;
var pathpfx = Path.Combine(certbase, "xxxxx.pfx");
var pathxml = Path.Combine(certbase, "metadata.xml");
.AddSaml2(options =>
{
options.SPOptions.EntityId = new EntityId("https://aaaa.aaa.com.tr");
options.SPOptions.ReturnUrl = new Uri("https://localhost:5003/Account/SignInPost");
options.SPOptions.MinIncomingSigningAlgorithm =
"http://www.w3.org/2000/09/xmldsig#rsa-sha1";
options.IdentityProviders.Add(
new IdentityProvider(
new EntityId("https://bbbbb.bbb.com.tr"), options.SPOptions)
{
MetadataLocation = pathxml,
LoadMetadata = true,
SingleSignOnServiceUrl = new Uri("https://bbbbb.bbb.com.tr/SAML/SSOService.aspx"),
Binding = Sustainsys.Saml2.WebSso.Saml2Binding.UriToSaml2BindingType(new
Uri("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect")),
SingleLogoutServiceUrl = new Uri("https://bbbbb.bbb.com.tr/SAML/SLOService.aspx"),
});
options.SPOptions.ServiceCertificates.Add(new X509Certificate2(pathpfx, "password"));
});
내 saml이 제대로 작동했지만 내 코드가 지금은 작동하지 않는 일이 발생했는지 모르겠습니다.
Chrome에서 코드를 실행할 때이 오류가 발생했습니다 =>
UnexpectedInResponseToException : 수신 된 메시지 _55ae387d-0b1e-4466-9b2b-d34a5254437c에 예기치 않은 InResponseTo "iddfcfd98961654500864ee897d823ce87"이 포함되어 있습니다. 요청에서 쿠키 보존 상태를 찾을 수 없으므로 메시지에 InResponseTo 속성이 없을 것으로 예상됩니다. 이 오류는 일반적으로 SP 시작 사인온을 수행 할 때 설정된 쿠키가 손실 된 경우 발생합니다.
내 metadata.xml은 괜찮습니다. 내 pfx도 괜찮습니다.
나는 2 일 이상 내 문제를 해결하기 위해 노력했지만 나를 도울 수있는 것을 찾을 수 없었다. 이 문제를 해결하기위한 당신의 제안은 무엇입니까? 당신이 나를 도울 수?
스택 내부
UnexpectedInResponseToException: Received message _63021ef9-2b93-4b0a-ab75-88d42e8a4c9c contains unexpected InResponseTo "idc0faf551a5bf421da3a9e582b57f685a". No cookie preserving state from the request was found so the message was not expected to have an InResponseTo attribute. This error typically occurs if the cookie set when doing SP-initiated sign on have been lost.
Sustainsys.Saml2.Saml2P.Saml2Response.ValidateInResponseTo(IOptions options, IEnumerable<ClaimsIdentity> claimsIdentities)
Sustainsys.Saml2.Saml2P.Saml2Response.GetClaims(IOptions options, IDictionary<string, string> relayData)
Sustainsys.Saml2.WebSso.AcsCommand.ProcessResponse(IOptions options, Saml2Response samlResponse, StoredRequestState storedRequestState, IdentityProvider identityProvider, string relayState)
Sustainsys.Saml2.WebSso.AcsCommand.Run(HttpRequestData request, IOptions options)
Sustainsys.Saml2.AspNetCore2.Saml2Handler.HandleRequestAsync()
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
답변
1 İbrahim Sep 02 2020 at 08:41
startup.cs에서 설정하십시오.
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.OnAppendCookie = cookieContext => SameSite.CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
options.OnDeleteCookie = cookieContext => SameSite.CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
//this is important. this will help you to solve the problem.
options.MinimumSameSitePolicy = SameSiteMode.None;
});
public void configure () {} 아래에이 메서드를 추가합니다.
public static class SameSite
{
public static void CheckSameSite(HttpContext httpContext, CookieOptions options)
{
if (options.SameSite == SameSiteMode.None)
{
if (!BrowserSupportsSameSiteNone(httpContext.Request.Headers["User-Agent"].ToString()))
{
// Unspecified - no SameSite will be included in the Set-Cookie.
options.SameSite = (SameSiteMode)(-1);
}
}
}
private static bool BrowserSupportsSameSiteNone(string userAgent)
{
// iOS 12 browsers don't support SameSite=None.
if (userAgent.Contains("CPU iPhone OS 12") || userAgent.Contains("iPad; CPU OS 12"))
{
return false;
}
// macOS 10.14 Mojave browsers don't support SameSite=None.
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") && userAgent.Contains("Version/") && userAgent.Contains("Safari"))
{
return false;
}
// Old versions of Chrome don't support SameSite=None.
if (userAgent.Contains("Chrome") || userAgent.Contains("Chrome/6"))
{
return false;
}
return true;
}
}