SAML SSO Redirection Behavior – ReturnUrl ignored, always posting to /Saml2/Acs instead of custom route

Niket Kumar Singh 540 Reputation points
2025-04-03T13:57:32.2033333+00:00

We're integrating SAML-based SSO using Azure AD as the Identity Provider (IdP) and the Sustainsys.Saml2 library (formerly Kentor.AuthServices) in a .NET 6 Web API backend hosted locally at https://localhost:5001.

Entity ID (Identifier ) : https://xxxxxxxxxxxxxxxxxx/a/webapi/api/externallogin/saml/identifier

Desired Assertion Consumer Service (ACS) URL: https://localhost:5001/api/Saml2/Acs

Azure AD Reply URLs (ACS) (both configured):

  • https://localhost:5001/api/Saml2/Acs
  • https://localhost:5001/Saml2/Acs

Despite configuring the desired ACS endpoint at /api/Saml2/Acs, the AuthnRequest is always generated with: AssertionConsumerServiceURL = https://localhost:5001/Saml2/Acs

As a result:

  • Azure AD may throw AADSTS50011: Reply URL mismatch if only the /api/... route is registered.
  • The browser gets redirected to /Saml2/Acs and shows a blank page or broken icon, because the request never reaches our custom controller.

Investigation Summary:

- In `Startup.cs`, we explicitly set:

  ```yaml
  
  options.SPOptions.ReturnUrl = new Uri("https://localhost:5001/api/Saml2/Acs");
  ```
  
- The expected behavior is that the `AssertionConsumerServiceURL` in the AuthnRequest should reflect this custom return URL.

- However, Sustainsys.Saml2 seems to ignore `SPOptions.ReturnUrl` for outgoing requests and always generates:

  ```yaml
  
  https://localhost:5001/Saml2/Acs
  ```
  
- Even using `options.Notifications.AcsCommandResultCreated` to manipulate the redirect doesn't help because the POST is still going to `/Saml2/Acs`, not `/api/Saml2/Acs`.

How can we override the ACS URL to be /api/Saml2/Acs when using Sustainsys.Saml2 in .NET 6?
Is there a recommended approach to force the SDK to honor SPOptions.ReturnUrl?

Do we need to extend or override AuthServicesUrls or SignInCommand to achieve this?

Would using MapSaml2 routes or switching to MVC-style routing offer more flexibility for handling this?

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
24,259 questions
{count} votes

Accepted answer
  1. Kancharla Saiteja 3,405 Reputation points Microsoft External Staff
    2025-04-09T17:06:24.5733333+00:00

    Hello @Niket Kumar Singh, I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue: SAML SSO Redirection Behavior – ReturnUrl ignored, always posting to /Saml2/Acs instead of custom route

    Solution: Resolved by @Niket Kumar Singh,

    Since the library did not allow ACS customization, we re-architected the flow without using Sustainsys and built a custom SAML response handler:

    • Configured Azure AD to point to our custom ACS endpoint: ✅ https://localhost:5001/api/Saml2/Acs
    • Implemented a controller endpoint in our .NET backend to directly receive and decode the SAML response.
    • Validated the SAML POST payload and extracted the necessary user claims (email, name, etc.).
    • Successfully verified the authentication flow end-to-end without relying on Sustainsys.
    • The application is now capable of handling the SAML response securely and flexibly and matches the custom ACS route expected in enterprise scenarios.

    If you have any other questions or are still running into more issues, please let me know. Thank you again for your time and patience throughout this issue.

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.