link

# Code example guide

Learn how to authenticate your end-users in just a few steps. You can use the Authentication API both to onboard new users and to authenticate registered users. This guide is designed to get you started quickly using the simplest use case, not to exhaustively cover everything the authentication solution can do. The back end is written in C# and the front end is written in React. The complete code example can be downloaded at the end of this guide.

See our API reference for detailed information about all the available endpoints and properties.

# 1. Set up Signicat

The easiest way to enable authentication in .NET is to use our SDK.

# Install via dotnet
dotnet add package Signicat.Express.SDK
dotnet restore
# Or install via NuGet
PM> Install-Package Signicat.Express.SDK

(Supports .NET Standard 2.0+, .NET Core 2.0+ and .NET Framework 4.6.1+)

# 2. Create a session

In the front end of your application, add an authentication button that calls a server-side endpoint to create an Identification Session.

const AuthDisplay = () => (
  <section>
    <h1> Welcome to the authentication process </h1>
    <div className="auth">
      <form action="/authentication-session" method="POST">
        <button type="submit">
          Sign in
        </button>
      </form>
    </div>
  </section>
);

In the back end of your application, define an endpoint that creates the session for your frontend to call. You need these values:

  • Your ClientId, ClientSecret and Identity listed in the OAuthScope. (If you don't have an account already, you can make a free Signicat account here (opens new window))
  • A Flow property. Here it is set to Redirect that will redirect the end-user to one of the URLs specified. Alternative options are Iframe and headless flow that you can read more about here.
  • Your SuccessUrl, ErrorUrl and AbortUrl, specifies where the end-user will be redirected after they complete the Identification Session.
  • The Include property requests information about the end-user. Here we want to retrieve the Name and Nin (National identification number).
[Route("authentication-session")]
[ApiController]
public class AuthenticationApiController : Controller
{
    private readonly IIdentificationV2Service _identificationV2Service;

    public AuthenticationApiController()
    {
        _identificationV2Service = new IdentificationV2Service("<ClientId>", "<ClientSecret>",
                new List<OAuthScope> { OAuthScope.Identify });
    }

    [HttpPost] 
    public async Task<ActionResult> Create()
    {
        var domain = "http://localhost:3000/";
        var backendDomain = "http://localhost:4242/";

        var session = await _identificationV2Service.CreateSessionAsync(new IdSessionCreateOptions()
        {
            Flow = IdSessionFlow.Redirect,
            RedirectSettings = new RedirectSettings()
            {
                ErrorUrl = domain + "?error=true",
                AbortUrl = domain + "?canceled=true",
                SuccessUrl = backendDomain + "authentication-session"
            },
            Include = new List<Include>()
            {
                Include.Name,
                Include.Nin
            }
        });

        Response.Headers.Add("Location", session.Url); 
        return new StatusCodeResult(303);
    }
}

# 3. Complete the authentication process

After creating the Identification Session, redirect the end-user to the Url returned in the response in order to complete the authentication process. The end-user can choose one of the allowed identification methods (read more about the supported identification methods here).

Id selection screen Id selection screen

# 4. Retrieve user data

After the authentication process is complete and the user is redirected to the specified successUrl the session and associated user data can be retrieved:

...

[HttpGet]
public async Task<ActionResult> Retrieve([FromQuery(Name = "sessionId")] string sessionId)
{
    var domain = "http://localhost:3000/";

    var result = await _identificationV2Service.GetSessionAsync(sessionId);
    string name = result.Identity.FullName;
    string nin = result.Identity.Nin;

    Response.Headers.Add("Location", domain + "?success=true&name=" + name + "&nin=" + nin);
    return new StatusCodeResult(303);
}

# Download pre-built code example

The complete code example includes functionality such as specifying identification methods, UI settings and handling information flow to the frontend. Please note that the example is built to be easy to understand and best practice is not always followed (e.g. sending National identification number in the URL).

# Video tutorial with walkthrough of Dashboard and MitID

Questions, comments, or suggestions? Check out our community (opens new window).

Last updated: 26/11/2021 11:56 UTC