0

I am writing an app with ASP.NET Core 8, the application is a SSO using SAML 2.0. The problem is that I am not very good using C# but my work is requiring it. I managed to get Jitbit's module to handle Saml. The mistake I have is at the level of the navigal and I do not know how to solve it:

Ypeerror [err_invalid_arg_type]: The First Argument Must Be of Type String or An Instance of Buffer, Arraybuffer, Or Array or An Array-Like Object. Undefined Received

I will provide all the code that I have so far

Controller:

namespace LoginSSOSAML.Controllers;

public class LoginController : Controller
{
    public IActionResult Login()
    {
        // TODO: specify the SAML provider url here, aka "Endpoint"
        var samlEndpoint = "https://mocksaml.com/api/saml/sso";

        var request = new AuthRequest(
            // TODO: put your app's "entity ID" here
            "http://loginssosaml.us-east-1.elasticbeanstalk.com/",

            // TODO: put Assertion Consumer URL (where the provider should redirect users after authenticating)
            "https://mocksaml.com/api/saml/sso"
        );

        return Redirect(request.GetRedirectUrl(samlEndpoint));
    }

    [HttpPost]
    public Task<IActionResult> SamlConsume()
    {
        string samlCertificate = @"-----BEGIN CERTIFICATE-----
BLAHBLAHBLAH
-----END CERTIFICATE-----";

        var samlResponseString = Request.Form["SAMLResponse"].ToString();
        var samlResponseBytes = Convert.FromBase64String(samlResponseString);
        var samlResponse = new Response(samlResponseBytes, samlCertificate);

        if (samlResponse.IsValid())
        {
            try
            {
                var username = samlResponse.GetNameID();
            }
            catch (Exception ex)
            {
                return Task.FromResult<IActionResult>(null!);
            }
        }

        return Task.FromResult<IActionResult>(Content("Unauthorized"));
    }
}

The login view:

@{
    ViewData["Title"] = "SSO Login";
}

<h1>Single Sign-On (SSO) Login</h1>

@if (User.Identity!.IsAuthenticated)
{
    <p>Welcome, @User.Identity.Name!</p>
    <p>First Name: @User.FindFirst(ClaimTypes.GivenName)?.Value</p>
}
else
{
    <p>You are not authenticated. Please log in using SSO.</p>
    <a asp-controller="Login" asp-action="Login">Login with SSO</a>
}

I really need to make it work, I can't use another language or authentication mode. I am using Mock Saml to do the tests and although it facilitates the metadata I still fail to make it work

1
  • From the code and the error you have provided it is difficult to identify from where the error is coming from. to troubleshoot the error first check SAMLResponse Key before using it in Request.Form. try to check that the samlResponseString is not null or undefined before attempting to convert it from a base64 string. add error handling code to get more detail from the code what is the actual issue or from where error is coming from Commented Jun 5 at 3:26

0