Build ASP.NET Core authentication with Logto
Learn how to build a user authentication flow with ASP.NET Core by integrating Logto SDK.
Learn how to build a user authentication flow with ASP.NET Core by integrating Logto SDK.
In this tutorial, we will show you how to build a user authentication flow with ASP.NET Core by integrating Logto SDK. The tutorial uses C# as the programming language.
Before you begin, ensure you have the following:
To get started, create a Logto application with the "Traditional web" type. Follow these steps to create a Logto application:
Then you should see an interactive tutorial that guides you through the process of integrating Logto SDK with your ASP.NET Core application. The following content can be a reference for future use.
Note the minimum supported version of ASP.NET Core is 6.0.
Open Startup.cs (or Program.cs) and add the following code to register Logto authentication middleware:
The AddLogtoAuthentication method will do the following things:
LogtoDefaults.CookieScheme.LogtoDefaults.AuthenticationScheme.LogtoDefaults.AuthenticationScheme.Add the following URI to the Redirect URIs list in the Logto application details page:
To sign-in with Logto, you can use the ChallengeAsync method:
The ChallengeAsync method will redirect the user to the Logto sign-in page.
The RedirectUri property is used to redirect the user back to your web application after authentication. Note it is different from the redirect URI you configured in the Logto application details page:
RedirectUri property is the URI that will be redirected to after necessary actions have been taken in the Logto authentication middleware.The order of the actions is 1 -> 2. For clarity, let's call the redirect URI in the Logto application details page the Logto redirect URI and the RedirectUri property the application redirect URI.
The Logto redirect URI has a default value of /Callback, which you can leave it as is if there's no special requirement. If you want to change it, you can set the CallbackPath property for LogtoOptions:
Remember to update the value in the Logto application details page accordingly.
Add the following URI to the Post sign-out redirect URIs list in the Logto application details page:
To sign-out with Logto, you can use the SignOutAsync method:
The SignOutAsync method will clear the authentication cookie and redirect the user to the Logto sign-out page.
The RedirectUri property is used to redirect the user back to your web application after sign-out. Note it is different from the post sign-out redirect URI you configured in the Logto application details page:
RedirectUri property is the URI that will be redirected to after necessary actions have been taken in the Logto authentication middleware.The order of the actions is 1 -> 2. For clarity, let's call the post sign-out redirect URI in the Logto application details page the Logto post sign-out redirect URI and the RedirectUri property the application post sign-out redirect URI.
The Logto post sign-out redirect URI has a default value of /SignedOutCallback, which you can leave it as is if there's no special requirement. If you want to change it, you can set the SignedOutCallbackPath property for LogtoOptions:
Remember to update the value in the Logto application details page accordingly.
First, add the handler methods to your PageModel, for example:
Then, add the buttons to your Razor page (html):
<p>Is authenticated: @User.Identity?.IsAuthenticated</p>
<form method="post">
@if (User.Identity?.IsAuthenticated == true)
{
<button type="submit" asp-page-handler="SignOut">Sign out</button>
} else {
<button type="submit" asp-page-handler="SignIn">Sign in</button>
}
</form>
It will show the "Sign in" button if the user is not authenticated, and show the "Sign out" button if the user is authenticated.
Now you can run the web application and try to sign-in/sign-out with Logto:
If you encounter any issues during the integration, please don't hesitate to contact us via email at [email protected] or join our Discord server!