This tutorial shows C# / .NET developers how to integrate passkeys into an ASP.NET Core app. Besides passkeys, simple session management is also implemented.
Nicolai
Created: October 17, 2023
Updated: October 1, 2024
We switched from web components to UI components. This blog post will soon be updated. Reach out via Slack if you need help.
In this blog post, we help C# / .NET developers to implement a sample
application with passkey authentication using ASP.NET Core. To make passkeys
work, we use Corbado's passkey-first web component that automatically
connects to a passkeys backend.
3. Repository structure for the ASP.NET Core passkey project
An ASP.NET Core project contains many files, but the important ones are the
following:
├── corbado-demo
| ├── api
| | ...
| | └── Pages
| | ├── Index.cshtml # The page which shows info about your profile when logged in
| | └── Login.cshtml # The login page
| |
| └── Properties
| └── launchSettings.json # Contains environment variables
4. Set up your Corbado account
and project
Visit the Corbado developer panel
to sign up and create your account (youll see a passkey sign-up in action
here!).
In the appearing project wizard, select Web app as type of app and
afterwards select No existing users, as were building a new app from
scratch. Moreover, providing some details regarding your frontend and backend
tech stack as well as the main goal you want to achieve with Corbado helps us
to customize and smoothen your developer experience.
Next, we navigate to Settings > General > URLs and set the
Application URL, Redirect URL and Relying Party ID to the following values (we will host our app on port
7283):
Application URL: Provide the URL where you embedded the web component, here: http://localhost:7283
Redirect URL: Provide the URL your app should redirect to after successful authentication and which gets sent a short-term session cookie, here: http://localhost:7283/
Relying Party ID: Provide the domain (no protocol, no port and no path) where passkeys should be bound to, here: localhost
Subscribe to our Passkeys Substack for the latest news, insights and strategies.
To initialize our project, we open Visual Studio and use the wizard to create
an ASP.NET Core web application:
Select as seen above and click Continue.
Select the target framework and use No Authentication as authentication
option.
Finally, give your project a name and a location and click Create.
Visual Studio will automatically open the project for you.
Ben Gould
Head of Engineering
I’ve built hundreds of integrations in my time, including quite a few with identity providers and I’ve never been so impressed with a developer experience as I have been with Corbado.
10,000+ devs trust Corbado & make the Internet safer with passkeys. Got questions? We’ve written 150+ blog posts on passkeys.
In the pages folder, we can delete all files except for "_ViewImports.cshtml"
and "_ViewStart.cshtml":
Then, we adjust the navigation in Pages/Shared/Layout.cshtml (We already added
the login and index routes here):
5.1 Configure environment variables
We will need the Corbado project ID in the next steps, so well add it as an
environment variable. For this, we edit the /Properties/launchSettings.json
file and paste our Corbado project ID:
5.2 Install NuGet packages
Make sure you have all of the following NuGet packages installed ("Tools >
Manage NuGetPackages" inside Visual Studio).
6. Create passkey login page
Right click the /Pages folder and select New file. Then select
ASP.NET Core > RazorPage (with page model) and click Create.
This will create a Login.cshtml file as well as a Login.cshtml.cs file.
Paste the following code into the Login.cshtml file:
Afterwards, add this code to the Login.cshtml.cs file:
Our login page contains the Corbado web component, which will handle
authentication.
7. Add index page
Like in step 6, we create a Razor page with page
model and call it Index which will result in Index.cshtml and
Index.cshtml.cs being created. After successful authentication, the Corbado
web component redirects the user to the provided Redirect URL
(http://localhost:7283/). This is the index page we just created. If someone
is logged in, it shows information about the user and provides a button to log
out. If no user is available, the page just displays an invitation to login.
For this, we add the following code to Index.cshtml:
7.1 Verify session
Before we can use information embedded in the session, we need to verify that
the session is valid. We therefore take the cbo_short_session cookie (the
session) and verify its signature using the public key from Corbado. This
happens in the Index.cshtml.cs file. We also verify that the issuer is correct
(remember to use your own project ID):
7.2 Get data from Corbado session
Finally, we can extract the information stored in the JWT
claims:
The variables "userID", "userName" and "userEmail" are used in the
Index.cshtml template from step 7.
8. Start
using passkeys with our ASP.NET Core implementation
To start our application, we head into Visual Studio and run the project (Hit
the play button or go to Debug -> Start Debugging).
Clicking on Login will lead you to the Login page:
After successful sign up / login, you see the following info on the Index
page:
9. Conclusion
This tutorial showed how easy it is to add passwordless authentication with passkeys to an
ASP.NET Core app using Corbado. Besides the passkey-first authentication,
Corbado provides simple session management, that we used for a retrieval of
basic user data. If you want to read more about how Corbado's session
management please check the docs here. If you want to add Corbado
to your existing app with existing users, please see our documentation
here.
Share this article
Table of Contents
Enjoyed this read?
🤝 Join our Passkeys Community
Share passkeys implementation tips and get support to free the world from passwords.