- Redirect users to Auth0 Universal Login when they sign in
- Handle the callback and store the session in a cookie
- Display the authenticated user’s name, email, and profile picture
- Sign users out of both your app and Auth0
This guide targets classic ASP.NET (.NET Framework) applications using OWIN. If your application already runs on ASP.NET Core, use the
Auth0.AspNetCore.Authentication SDK instead.Prerequisites
Before you begin:- An Auth0 account - sign up for free
- An existing ASP.NET MVC application targeting .NET Framework with OWIN enabled, or a new one created from the ASP.NET Web Application (.NET Framework) → MVC template in Visual Studio
- Visual Studio 2019 or later (or any IDE that supports .NET Framework MVC projects)
Steps
Configure your Auth0 application
Every application that uses Auth0 needs to be registered in the Auth0 Dashboard. Auth0 issues a Client ID and Domain that your app uses to communicate with Auth0.You can create and configure the Auth0 application automatically using the Auth0 CLI, or manually via the Dashboard:
- CLI
- Dashboard
Run the following command from your project’s root directory. It creates an Auth0 application and outputs a ready-to-paste Copy the two
Web.config snippet with your credentials filled in:- Mac/Linux
- Windows (PowerShell)
<add> lines from the output into your Web.config <appSettings> section:Web.config
If you’re running your app on a different port, replace
3000 with your actual port number in every URL above.Install NuGet packages
Add the two required OWIN middleware packages to your project:
| Package | Purpose |
|---|---|
Microsoft.Owin.Security.OpenIdConnect | Handles the OpenID Connect (OIDC) authentication flow with Auth0 |
Microsoft.Owin.Security.Cookies | Persists the user session in a browser cookie after login |
- Package Manager Console
- dotnet CLI
In Visual Studio, open the Package Manager Console (
Tools → NuGet Package Manager → Package Manager Console) and run:Running OWIN cookie middleware alongside
System.Web cookies can cause issues. If you encounter double-cookie problems, see the System.Web cookie integration issues guidance.Configure OWIN middleware
OWIN middleware is registered in a startup class. If your project already has an OWIN startup class (commonly Make sure
App_Start/Startup.Auth.cs), update its ConfigureAuth method. If not, create the file now.Both cookie middleware and OpenID Connect middleware are required, and they must be registered in this exact order:- Cookie middleware - stores the authenticated user session
- OpenID Connect middleware - handles the Auth0 login and logout flow
App_Start/Startup.Auth.cs
ConfigureAuth is called from your Startup.cs Configuration method:Startup.cs
AuthenticationType is set to "Auth0". This string is used in the next step when triggering the login challenge. The RedirectToIdentityProvider notification intercepts logout requests and builds the correct Auth0 logout URL.Add login, logout, and profile actions
Create How each action works:
Controllers/AccountController.cs with three actions: Login, Logout, and UserProfile.Controllers/AccountController.cs
Login- callsChallengewith the"Auth0"scheme. The OIDC middleware intercepts this and redirects the user to Auth0 Universal Login. After a successful sign-in, the user is redirected back toreturnUrl.UserProfile- reads the authenticated user’s claims fromClaimsIdentityand passes them to the view viaUserProfileViewModel. The[Authorize]attribute ensures unauthenticated users are redirected to login first.Logout- callsSignOuttwice: once to clear the local cookie session, and once to sign the user out of Auth0 (which also ends any active SSO sessions).
Models/UserProfileViewModel.cs to hold the profile data:Models/UserProfileViewModel.cs
Checkpoint
Run your application and navigate to/Account/Login. You should be redirected to the Auth0 Universal Login page. After signing in, you should be redirected back to your application’s home page. If you see a redirect URI error, verify that the callback URL in your Auth0 application settings exactly matches the URL your application is running on.Add a profile view
Create The view receives a
Views/Account/UserProfile.cshtml to display the signed-in user’s information:Views/Account/UserProfile.cshtml
UserProfileViewModel populated from claims extracted by the OIDC middleware when Auth0 returns the ID token.Checkpoint
After logging in, navigate to/Account/UserProfile. You should see your name, email, and profile picture. If the name or email appears empty, verify that the Scope in your OpenIdConnectAuthenticationOptions includes "openid profile email".Add login and logout links to your layout
Update Add this inside the
Views/Shared/_Layout.cshtml to show login and logout links based on the user’s authentication state:Views/Shared/_Layout.cshtml
<nav> element wherever your navigation links appear in the layout.Checkpoint
Run your application. You should see a Log in link in the navigation. After signing in, it should change to your name (linking to your profile) and a Log out link. Clicking Log out should sign you out and return you to the home page.You now have a working Auth0 integration in your ASP.NET OWIN application. Users can log in through Auth0 Universal Login, view their profile, and log out.
Common Issues
Redirect URI mismatch after login
Redirect URI mismatch after login
Problem: Auth0 shows a “redirect_uri mismatch” or “callback URL mismatch” error after the user signs in.Solution: The redirect URI your app sends to Auth0 must exactly match one of the Allowed Callback URLs in your Auth0 application settings. Check for differences in protocol (
http vs https), port number, path, and trailing slashes.Login loop — app keeps redirecting back to Auth0
Login loop — app keeps redirecting back to Auth0
Problem: After signing in successfully, the app immediately redirects back to Auth0 instead of showing the authenticated page.Solution: Ensure middleware is registered in the correct order and the OWIN pipeline is initialized:
- Cookie middleware must be registered before OpenID Connect middleware in
ConfigureAuth. app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType)must be the first call inConfigureAuth.- The
[assembly: OwinStartup(typeof(Startup))]attribute must be present so the OWIN pipeline is correctly initialized.
App_Start/Startup.Auth.cs
User is not redirected back after logout
User is not redirected back after logout
Problem: Clicking Log out signs the user out of Auth0 but doesn’t return them to your application.Solution: Add a
returnTo query parameter to the Auth0 logout URL in the RedirectToIdentityProvider notification. The return URL must also be listed in Allowed Logout URLs in your Auth0 application settings:App_Start/Startup.Auth.cs
Profile picture or email is empty
Profile picture or email is empty
Problem:
Model.ProfileImage or Model.EmailAddress is null after login.Solution: Verify that Scope in OpenIdConnectAuthenticationOptions includes "openid profile email". The profile scope provides name and picture; the email scope provides the email address.App_Start/Startup.Auth.cs
Domain or Client ID values are null at startup
Domain or Client ID values are null at startup
Problem: The application throws a null reference or configuration exception when starting.Solution: Confirm both
auth0:Domain and auth0:ClientId exist in <appSettings> in Web.config, and that you are running the correct build configuration (Debug/Release) that loads the right Web.config transform.Web.config
Advanced Usage
Customize Login Parameters
Customize Login Parameters
You can pass custom parameters to the Auth0 login page by modifying the
RedirectToIdentityProvider notification in Startup.Auth.cs:App_Start/Startup.Auth.cs
Call an API on Behalf of the User
Call an API on Behalf of the User
To call an API with an access token, request an Then retrieve the access token from the authenticated user’s claims:
audience and the required API scopes during the OIDC redirect:App_Start/Startup.Auth.cs
Controllers/ApiController.cs
Additional Resources
Sample Application
Complete working example of this quickstart
Katana / OWIN Documentation
Microsoft’s official OWIN/Katana reference
Community Forum
Get help from the Auth0 community