OpenID Connect from ASP.NET Core with Visual Studio 2017
Adding OpendID Connect authentication to you ASP.NET Core web site is easy. In this example with Visual Studio 2017, it’s basically just a few clicks and a few lines of code and you will be up and running with Swedish BankID or some of the other e-ID’s out there.
The steps shown here creates a site from scratch, but the instructions may as well be applied to an existing application.
And hey, if you are on a Mac check this cool example of the same thing, but with no Windows involved!
Four steps are involved in getting going from zero:
- You’ve already got VS 2017 and ASP.Net Core set up on your computer. That was simple! Or you don’t and then you read this post from Microsoft to learn how.
- Create your basic ASP.NET Core application as a starting point
- Modify your application to defer authentication to easyID.
and to make it all work with the BankIDs you need to
- Set up an account with easyID to enable OpenID Connect on top of the national identities.
Create your basic ASP.NET Core web application
To create you new web site just create a new project in VS 2017 and choose the ASP.NET Core Web Application
Next you just pick Web Application with no authentiation.
Your web site is set up and you are ready to build and run this basic web site with nothing but an Ctrl-F5. Go ahead, give it a try!
Modify you ASP.NET Core app for OpenID Connect
So you’re up an running at you see something like this:
Nice starting point, but no authentication, so let’s add some plumbing to sign in users with Swedish BankID on their mobile phone.
Next up is adding the references to the OpenID Connect stuff. Open the NPM Package Manager Console to install the packages:
This will add the needed packages, and you may rebuild once the package restore process has completed.
Next we will add the initialization code to the Startup class (in
Configuration method add these lines after the
options object sets the OpenID Connect middelware behaviour.
AuthorizationScheme property determines the callback you
must register with your OpenID Connect identity provider (see the
section at the end on how to do that for easyID).
In this case we set it to
oidc which means the callback will be on
/signin-oidc. Still, this is handled by the middelware so no need for
any additional code.
For easyID, the
ClientSecret properties reference your
domain, client ID and client secret. If you’re in a hurry to try this out, you may go with
the values provided in the comments next to the properties.
Also note the
AcrValues (authentication context reference) parameter
event handler. This is needed for easyID which supports several different
identity services on the same endpoint. If you use a simple OIDC identity
provider e.g. Google, you will not need this event handler.
For easyID the
AcrValues identifies the specific kind of authentication
you choose. Your options at the time of writing include:
- Norwegian BankID:
- Hardware token (kodebrikke):
- Swedish BankID:
- Same device:
- Another device (aka mobile):
- Same device:
- Danish NemID:
- Personal with code card:
- Employee with code card:
- Employee with code file:
- Personal with code card:
We are now ready to authenticate. Before trying it out we just need a protected resource that will display user information.
The protected view: Start the login and show the user info
First add a login link to the front page. Put a login link in the menu,
or something to the same effect, in the
_Layout.cshtml shared view where the
top menu is rendered:
To implement the
Protected view which will kick off the authentication process,
add a new action to the
HomeController. Notice the
which will start the OIDC flow.
Now, adding a simple view,
Protected.cshtml, in the
Views/Home to display the claims, and we are set.
Running the application
To execute an a login flow, remember to set the
ClientSecret. If you haven’t already set up an easyID account,
go to last section to do that. Or simply run with the test values
given in the comments in the code snippet further up.
One thing to remember: If you choose to just run with the test values for
do remember to set the port number of your web application to
50418, as this value is already
registered with our test tenant in easyID.
Hit F5 and you’re off. Once the front page has opened up click the Login menu at the top.
Setting up your easyID account
As the various national and bank identity services do not support OpenID Connect or any other standard protocol, we use Grean easyID as the identity service in the middle.
So, if you haven’t done so already, sign up for easyID: grean.com/easyid
A note on test users
In order to test the use of Norwegian BankID, Swedish BankID, and Danish NemID you need test identities.
For the sample shown above, we’ve set it up to use Swedish BankID (on the same device as your browser). As shown earlier you may easily switch to one of the other supported identities.
To obtain a test identity for Swedish BankID, go to https://demo.bankid.com and create the test¸identities you need. (Note that to install a test identity on your phone you will have to follow the guidelines on the site).
Also, please let us know if you need help getting your own test identities, and we will help you set them up. Just go to grean.com/easyid and sign up. Once signed up you can join our Slack channel from the easyID dashboard.
Setting up your easyID account
Name your first domain which will be a subdomain of grean.id. This will be the domain name we will use in the following.
Once signed up, go to the Applications tab to register your application.
Note that if you use Auth0 as your identity broker your life just got a little bit easier and you should take a look at our screencast showing how that scenario works.
But this post is not about that, so for this exercise just go for the manual option.
Remember to copy the Client ID value - you will need it when configuring the OpenID Connect middleware.
For the Callback URLs just enter the URL where your application may be reached for notification of the
authenticated user. In our example that would be
The actual portnumber may be picked - or set - in the properties of you web site in Visual Studio.
If you use the test values provided in the code blocks, remember to set you application’s portnumber to
Once the application has been registered in easyID, you have one more step to get it set up for OpenID Connect. Open the easyID application registration again and switch on the OAuth2 Code flow. This will generate a client secret that you will need in your application. Be sure to copy the value or you will have to generate a new one. (As with any other password we will, of course, only store the hashed value)
With this you will be able to set up your application to authenticate using any of the enabled identity services: Danish NemID, Swedish BankID and/or Norwegian BankID.
Go to grean.com to sign up today to quickly enable your applications to accept real people’s real identities. We run a tight ship to ensure that you get the same level of security and safety that you get from the various proprietary bank and government solutions!