Trouble validating OpenIdConnectAuthentication setup for Azure AD integration in ASP.NET OWIN application

36 Views Asked by At

I'm working on an ASP.NET application using OWIN and trying to set up OpenID Connect authentication for integration with Azure AD. The goal is to connect via my Azure credentials to edit Microsoft Office files on the web.

Here's the relevant code snippet where I attempt to set up the OpenID Connect authentication:

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
using System.Configuration;

[assembly: OwinStartup(typeof(MS_office_Read_WriteV3.App_Start.ReadWriteStartUp))]
//[assembly: OwinStartup("ReadWriteConfiguration", typeof(MS_office_Read_WriteV3.App_Start.ReadWriteStartUp))]
namespace MS_office_Read_WriteV3.App_Start
{
    public class ReadWriteStartUp
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "Cookies",
            });

            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ClientId = ConfigurationManager.AppSettings["ida:ClientId"],
                Authority = ConfigurationManager.AppSettings["ida:Authority"],
                RedirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"],
                ResponseType = "id_token",
                Scope = "openid profile",
                SignInAsAuthenticationType = "Cookies",

                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = context =>
                    {
                        // Log claims for debugging
                        LogClaims((ClaimsIdentity)context.AuthenticationTicket.Identity.Claims);

                        // Add custom claims or logic after successful token validation
                        return Task.CompletedTask;
                    }
                }
            });
        }

        private void LogClaims(ClaimsIdentity claimsIdentity)
        {
            foreach (var claim in claimsIdentity.Claims)
            {
                // Log or debug output
                System.Diagnostics.Debug.WriteLine($"Claim Type: {claim.Type}, Value: {claim.Value}");
            }
        }
    }
}

Despite this, I'm not getting the expected success message, and the SecurityTokenValidated event doesn't seem to be triggered during the authentication flow. I've set breakpoints and inspected the code, but I'm unable to identify the issue.

Additionally, I'm connecting to Azure AD via my credentials to enable editing Microsoft Office files on the web.

Any insights or guidance on how to properly validate the setup of OpenIdConnectAuthentication and troubleshoot why the SecurityTokenValidated event might not be firing in the context of connecting to Azure AD for editing Microsoft Office files on the web would be greatly appreciated.

Thanks in advance!

0

There are 0 best solutions below