Skip to content

okta-samples/okta-aspnet-core3-sample

Repository files navigation

ASP.NET Core & Okta-Hosted Sign-In Page Example

This example shows you how to use the Okta ASP.NET Core SDK to sign in a user. The user's browser is first redirected to the Okta-hosted sign-in page. After the user authenticates, they are redirected back to your application. ASP.NET Core automatically populates HttpContext.User with the information Okta sends back about the user.

Prerequisites

Before running this example, you will need the following:

  • An Okta Developer Account, create one using okta register, or configure an existing one with okta login

Running This Example

Grab and configure this project using okta start aspnet-core3

Run the web application

Run the example with your preferred tool and write down the port of your web application to configure Okta afterwards.

NOTE: This sample is using ASP.NET Core 3.1 which enforces HTTPS. This is a recommended practice for web applications. Check out Enforce HTTPS in ASP.NET Core for more details.

Because of recent changes in Set-Cookie behavior (SameSite) this code will only work properly if it's configured to use https. Check out Work with SameSite cookies in ASP.NET Core for more details.

Run the web application from Visual Studio

If you run this project in Visual Studio it will start the web application on port 44314 using HTTPS. You can change this configuration in the launchSettings.json in the Properties folder.

Run the web application from dotnet CLI

If you run this project via the dotnet CLI it will start the web application on port 5001 using HTTPS. You can change this configuration in the launchSettings.json in the Properties folder.

Navigate to the folder where the project file is located and type the following:

dotnet run

Trust the local dev certificate if necessary

If you’ve never run an ASP.NET Core 3.x application before, you may notice a strange error page come up warning you that the site is potentially unsafe. This is because ASP.NET Core creates an HTTPS development certificate for you as part of the first-run experience, but it still needs to be trusted. You can ignore the warning by clicking on Advanced and telling the browser that it’s okay to visit this site even though there is no certificate for it. Or you can trust the certificate to get rid of this warning, check out Configuring HTTPS in ASP.NET Core across different platforms for more details.

Click the Sign In link in the Home page and it will redirect you to the Okta hosted sign-in page.

You can sign in with the same account that you created when signing up for your Developer Org, or you can use a known username and password from your Okta Directory.

Note: If you are currently using your Developer Console, you already have a Single Sign-On (SSO) session for your Org. You will be automatically signed into your application as the same user that is using the Developer Console. You may want to use an incognito tab to test the flow from a blank slate.