OpenID Connect from ASP.NET Core - on Mac OSX
Sometimes you do stuff becuase you can. This is sort of one of those accounts. But hopefully one that will gain relevance as .NET Core and ASP.NET Core moves to the mainstream. And to make it more fun we will do it all on Mac OSX. Open house, no Windows!
Here we will authenticate your users with Norwegian or Swedish BankID or Danish NemID using ASP.NET Core on Mac OSX. By leveraging OpenID Connect, connecting ASP.NET Core applications to national identity services via easyID is a trivial job, as shown in this post.
To immediately get a running version of the code from this post get the sample from GitHub
The steps shown here creates a site from scratch, but the instructions may as well be applied to an existing application.
Three steps are involved in getting going from zero:
- Get set up for .NET Core and ASP.NET Core on your Mac
- Modify your ASP.NET Core 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.
1. Get ready for ASP.NET Core on Mac OSX
First off, you must install a version of .NET Core with support for the MS Build project format. Not the project.json format which is being abondoned. This took me some time, and with limited time on my hands a few of those “whatever works” moments.
So at the time of writing this meant I had to go here to get a working .NET Core downlaod that would work with the ASP.NET Core Yeoman template. But that will change once things fall into place and all the ducks line up.
Once you have the right version of .NET you want to install Yeoman and the Yeoman ASP.NET Core generator. Assuming you already have NPM installed, this is simple:
and then, to generate the fully functional ASP.NET site, run Yeoman and answer the questions asked along the way.
Web Application Basic [without ...]
Once the site is in place go into the application directory go through the .NET restore, build, and run motions:
You can now watch your new ASP.NET Core site on your Mac by going to localhost:5000.
2. Modify you ASP.NET Core app for OpenID Connect
So you’re up an running at you see something like this:
That’s lovely, on a Mac and all. But no authentication, so let’s add some plumbing to sign in users with Swedish BankID.
Next up is adding the references to the JWT and OpenID stuff. Open the
.csproj file and add the three last lines, the three package references:
This will add the new references, but you still need to retrieve the referenced packages:
Now we have the packages and infrastructure components we need.
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.
Now all that remains is to run the site:
Now navigate to http://localhost:5000 and click the Login menu at the top.
That’s it! Or if you didn’t actually go through the steps, you may view, download, and run this sample from GitHub
3. 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
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 criipto.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!
RECENT POSTSJune 15, 2017
OpenID Connect from ASP.NET Core with Visual Studio 2017March 24, 2017
Sign text with BankID, NemID, right in your front-endFebruary 25, 2017
OpenID Connect from ASP.NET Core - on Mac OSXFebruary 14, 2017
Use BankID and NemID from a Node.js applicationJanuary 10, 2017
Security considerations when building an identity serviceDecember 15, 2016
Real world identities online: Why and how?December 07, 2016
Criipto Verify connects Auth0 to Scandinavian national and bank identitiesDecember 04, 2015
Grean extends Auth0 with B2B onboarding and authorizationAugust 31, 2015
Are security concerns holding back your B2B integration?