如何使用 OWIN 从 Asp.Net MVC 访问 Auth0 用户配置文件中的用户名

发布于 2025-01-12 21:18:34 字数 2737 浏览 1 评论 0原文

我刚刚学习 Auth0,并且正在使用 此处

我注意到,ClaimsIdentity 可以通过在startup.cs 中的Auth0 配置中定义对范围的访问来访问某些用户信息,例如个人资料和电子邮件,如下所示:

// 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://learn.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);
                }


            }

我想做的是检索用户名字段。我假设用户名将是配置文件范围的一部分,但事实并非如此。我尝试将用户名添加到范围定义中 (Scope = "openid profile email username"),但这不起作用。

有谁知道我如何访问用户名字段?

谢谢

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 = 
quot;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://learn.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 = 
quot;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 += 
quot;&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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文