I created the whole process to use user authentication in my ASP.NET Core 6 MVC application, through Azure Active Directory. I'm using the Microsoft.Identity.Web API for this.
I can open the login screen, log in, but the callback (CallbackPath
) is failing.
Here are parts of code and result with error after login:
Startup.cs
foreach (var conn in azureADTenants)
{
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
options.Instance = conn.ActiveDirectorySettings.Instance;
options.Domain = conn.ActiveDirectorySettings.Domain;
options.TenantId = conn.ActiveDirectorySettings.TenantId;
options.ClientId = conn.ActiveDirectorySettings.ClientId;
options.ClientSecret = conn.ActiveDirectorySettings.ClientSecret;
options.CallbackPath = conn.ActiveDirectorySettings.CallbackPath;
options.SignedOutCallbackPath = conn.ActiveDirectorySettings.SignedOutCallbackPath;
});
services.Configure<OpenIdConnectOptions>(conn.Name, options =>
{
var existingOnTokenValidatedHandler = options.Events.OnTokenValidated;
options.Events.OnTokenValidated = async context =>
{
await existingOnTokenValidatedHandler(context);
await context.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
context.Principal);
};
});
}
appSettings.json
"ActiveDirectorySettings": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "aaahotmail.onmicrosoft.com",
"TenantId": "xxxxxxxx-xxx...",
"ClientId": "xxxxxxxx-xxx...",
"ClientSecret": "asasasasasas",
"CallbackPath": "/Login/signin-oidc",
"SignedOutCallbackPath": "/Login/signout-oidc"
}
In the Azure portal, in the redirect URIs section I entered:
https://localhost:81/Login/signin-oidc
In my controller class I added the redirect action
[Authorize]
[ActionName("signin-oidc")]
public IActionResult SignInRedirectFromActiveDirectory()
{
return null;
}
But I'm getting it in return:
So, what in practice do I need to do more to have login redirection in my controller (LoginController
)?
Thank you very much in advance for your help!
CodePudding user response:
I got something to no longer receive the error I mentioned.
The system still doesn't redirect to my controller, but passes authentication in AD and then returns to my login controller (where I originally called /Login/Index).
In startup.cs i added the following:
Ref: https://www.youtube.com/watch?v=S_xDAB_s-GM&list=WL&index=3
Thank you for your help