I'm trying to implement Entra Active Directory in my ASP.NET MVC application. Previously, the app used OWIN authentication to logging in to the system.
App is running on .NET Framework 4.8.
So for the Entra login I have updated the NuGet packages to latest one and added some extra packages.
Microsoft.Owin -Version 4.2.2
Microsoft.Owin.Host.SystemWeb -Version 4.2.2
Microsoft.Owin.Security -Version 4.2.2
Microsoft.Owin.Security.Cookies -Version 4.2.2
Microsoft.Owin.Security.OpenIdConnect -Version 4.2.2
Microsoft.IdentityModel.Protocols.OpenIdConnect -Version 8.3.0
Microsoft.IdentityModel.Tokens -Version 8.3.0
Next I have updated the Startup.Auth.cs
class:
using System;
using System.Configuration;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Owin;
using TorquexMediaPlayer.Models;
namespace TorquexMediaPlayer
{
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// These values can be stored in Web.config or appSettings.json
string clientId = ConfigurationManager.AppSettings["ClientId"];
string clientSecret = ConfigurationManager.AppSettings["ClientSecret"];
string tenantId = ConfigurationManager.AppSettings["TenantId"];
// e.g., "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" or "mytenant.onmicrosoft.com"
// Authority format: "https://login.microsoftonline.com/{tenantId}/v2.0"
string authority = $"https://login.microsoftonline.com/{tenantId}/v2.0";
// CookieAuthentication must be enabled for OWIN to store user information after sign-in
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
// Configure the OpenIdConnect middleware
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = "https://localhost:44300/signin-oidc",
PostLogoutRedirectUri = "https://localhost:44300/",
Scope = OpenIdConnectScope.OpenIdProfile,
ResponseType = OpenIdConnectResponseType.CodeIdToken,
// Use the client secret to authenticate your app
ClientSecret = clientSecret,
// Token validation parameters
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
// For single-tenant apps, validate the issuer to be your specific tenant
ValidIssuer = $"https://login.microsoftonline.com/{tenantId}/v2.0"
},
// Notification handlers
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
// Handle authentication failures
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return System.Threading.Tasks.Task.FromResult(0);
}
}
});
}
}
But when I add all packages to the class I got these errors
'IAppBuilder' does not contain a definition for 'UseOpenIdConnectAuthentication' and no accessible extension method 'UseOpenIdConnectAuthentication' accepting a first argument of type 'IAppBuilder' could be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'OpenIdConnectAuthenticationOptions' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'OpenIdConnectAuthenticationNotifications' could not be found (are you missing a using directive or an assembly reference?)
I tried to uninstall all related packages and re-installed them, but the issue still persists.
I also tried to downgrade the packages, but the issue persists.
How to solve this issue?
The error you're encountering due to incompatible package versions.
Microsoft.IdentityModel.Protocols.OpenIdConnect
and Microsoft.IdentityModel.Tokens
Versions 8.3.0
are designed for use with newer frameworks.
8.3.0
to 5.3.0
Microsoft.Owin -Version 4.2.2
Microsoft.Owin.Host.SystemWeb -Version 4.2.2 Microsoft.Owin.Security -Version 4.2.2
Microsoft.Owin.Security.Cookies -Version 4.2.2
Microsoft.Owin.Security.OpenIdConnect -Version 4.2.2
Microsoft.IdentityModel.Protocols.OpenIdConnect -Version 5.3.0
Microsoft.IdentityModel.Tokens -Version 5.3.0
bin
and obj
folders to remove old files.clear All NuGet Storage.
After making the above changes, I was able to successfully authenticate my ASP.Net MVC
application.
Add the below line at the top of StartupAuth.cs
file to specify the class that configures the middleware when the application starts.
[assembly:OwinStartup(typeof(versioncheckapp.Startup))]
My StartupAuth.cs
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
[assembly: OwinStartup(typeof(versioncheckapp.Startup))]
namespace versioncheckapp
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
public void ConfigureAuth(IAppBuilder app)
{
string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];
string tenantId = System.Configuration.ConfigurationManager.AppSettings["TenantId"];
string clientSecret = System.Configuration.ConfigurationManager.AppSettings["ClientSecret"];
string authority = $"https://login.microsoftonline.com/{tenantId}/v2.0";
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = "https://localhost:44394/signin-oidc",
PostLogoutRedirectUri = "https://localhost:44394/",
Scope = OpenIdConnectScope.OpenIdProfile,
ResponseType = OpenIdConnectResponseType.CodeIdToken,
ClientSecret = clientSecret,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = $"https://login.microsoftonline.com/{tenantId}/v2.0"
},
UsePkce = true,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Home/Error?message=" + context.Exception.Message);
return System.Threading.Tasks.Task.FromResult(0);
}
}
});
}
}
}
I've successfully authenticated to Azure AD
Output: