How do I access the username in an Auth0 user profile from Asp.Net MVC using OWIN

Viewed 94

I am just learning Auth0 and I am using the sample Asp.Net MVC app from here.

I note that the ClaimsIdentity can access certain user information such as profile and email by defining access to scopes in the Auth0 configuration in startup.cs as follows:

// Configure Auth0 authentication
        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            AuthenticationType = "Auth0",

            Authority = $"https://{auth0Domain}",

            ClientId = auth0ClientId,

            RedirectUri = auth0RedirectUri,
            PostLogoutRedirectUri = auth0PostLogoutRedirectUri,

            // This is where the Scopes are defined
            Scope = "openid profile email",

            TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = "name",
                RoleClaimType = "https://schemas.quickstarts.com/roles"
            },

            // More information on why the CookieManager needs to be set can be found here: 
            // https://docs.microsoft.com/en-us/aspnet/samesite/owin-samesite
            CookieManager = new SameSiteCookieManager(new SystemWebCookieManager()),

            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                RedirectToIdentityProvider = notification =>
                {
                    if (notification.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout)
                    {
                        var logoutUri = $"https://{auth0Domain}/v2/logout?client_id={auth0ClientId}";

                        var postLogoutUri = notification.ProtocolMessage.PostLogoutRedirectUri;

                        if (!string.IsNullOrEmpty(postLogoutUri))
                        {
                            if (postLogoutUri.StartsWith("/"))
                            {
                                // transform to absolute
                                var request = notification.Request;
                                postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
                            }
                            logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}";
                        }

                        notification.Response.Redirect(logoutUri);
                        notification.HandleResponse();
                    }
                    return Task.FromResult(0);
                }


            }

What I would like to do is retrieve the username field. I assumed the username would be part of the Profile scope but it is not. I tried adding username to the Scope definition (Scope = "openid profile email username") but this didn't work.

Does anyone know how I access the username field?

Thanks

0 Answers
Related