Introduction
Microsoft Entra ID (formerly Azure Active Directory) provides authentication and authorization capabilities for modern applications. Integrating Entra ID with a .NET application using OWIN (Open Web Interface for .NET) middleware allows seamless authentication using industry-standard protocols such as OpenID Connect and OAuth 2.0.
Use case
Most of the legacy applications use the .NET Framework, and the Enterprise will look to integrate the Entra ID as an Identity provider into these applications for the SSO, as part of cloud adoption. In this use case, sometimes you may not get time to modernize the legacy application, or you may prefer a phase-by-phase approach to modernize the application. In this case, you can use OWIN libraries in your .NET Framework application to integrate with Microsoft Entra ID for Single sign-on.
In this article, I will explain how to integrate Microsoft Entra ID with a .NET application using OWIN libraries.
Step 1. Register the Application in Microsoft Entra ID.
To enable authentication, you need to register your application using the Microsoft Entra ID portal.
- Go to the Microsoft Entra ID portal.
- Navigate to App registrations > New registration.
- Enter a name for your application.
- Set the Supported account types (Single Tenant or Multi-Tenant).
- Specify the Redirect URI (e.g., https://localhost:44300/signin-oidc).
- Click Register.
- Copy the Application (client) ID and Directory (tenant) ID.
Step 2. Install Required OWIN Packages.
In your .NET Framework application, install the necessary OWIN NuGet packages by running the following commands in the Package Manager Console.
Install-Package Microsoft.Owin.Security.OpenIdConnect
Install-Package Microsoft.Owin.Security.Cookies
Install-Package Microsoft.Owin.Host.SystemWeb
Step 3. Configure OWIN Middleware.
In the Startup.cs file, configure OWIN to use Microsoft Entra ID authentication.
Create a Startup.Auth.cs file under the app start folder if it does not exist and put the code below.
[assembly: OwinStartup(typeof(EntraId_Owin_Net_Framework.Startup))]
namespace EntraId_Owin_Net_Framework
{
public partial class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string aadInstance = EnsureTrailingSlash(ConfigurationManager.AppSettings["ida:AADInstance"]);
private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
private static string RedirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
private static string authority = aadInstance + tenantId + "/v2.0";
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = RedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
string name = context.AuthenticationTicket.Identity.FindFirst("preferred_username").Value;
context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimTypes.Name, name, string.Empty));
return System.Threading.Tasks.Task.FromResult(0);
}
}
});
}
private static string EnsureTrailingSlash(string value)
{
if (value == null)
{
value = string.Empty;
}
if (!value.EndsWith("/", StringComparison.Ordinal))
{
return value + "/";
}
return value;
}
}
}


Join the conversation! Your thoughts help the community grow.