# 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. Install Signicat Express SDK
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
andIdentity
listed in theOAuthScope
. (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 toRedirect
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
andAbortUrl
, 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 theName
andNin
(National identification number).
[Route("authentication-session")]
[ApiController]
public class AuthenticationApiController : Controller
{
private readonly IIdentificationV2Service _identificationService;
private readonly string _frontendAppUrl;
private readonly string _backendUrl;
public AuthenticationApiController(IIdentificationV2Service identificationService, IConfiguration configuration)
{
_identificationService = identificationService;
_frontendAppUrl = configuration["FrontendAppUrl"];
_backendUrl = configuration["BackendUrl"];
}
[HttpPost]
public async Task<ActionResult> Create()
{
var session = await _identificationService.CreateSessionAsync(new IdSessionCreateOptions()
{
Flow = IdSessionFlow.Redirect,
RedirectSettings = new RedirectSettings()
{
ErrorUrl = _frontendAppUrl + "?error=true",
AbortUrl = _frontendAppUrl + "?canceled=true",
SuccessUrl = _backendUrl + "authentication-session"
},
AllowedProviders = new List<IdProviderType>
{
IdProviderType.Mitid,
IdProviderType.DkNemid,
IdProviderType.NoBankidNetcentric,
IdProviderType.NoBankidMobile,
IdProviderType.SeBankid,
IdProviderType.SmsOtp,
IdProviderType.NoBuypass,
},
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).
# 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 result = await _identificationService.GetSessionAsync(sessionId);
string name = result.Identity.FullName;
string nin = result.Identity.Nin;
var encodedName = Uri.EscapeDataString(name);
Response.Headers.Add("Location", _frontendAppUrl + "?success=true&name=" + encodedName + "&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).
Or check out our GitHub page: github.com/signicat/sample-express-authentication/tree/main/RestAPI (opens new window).
# Video tutorial with walkthrough of Dashboard and MitID
Questions, comments, or suggestions? Check out our community (opens new window).