.net - 如何获取Azure native 客户端应用程序角色?

标签 .net azure authentication adal role

我正在将 WinForms 应用程序验证为可以访问 Azure 托管服务 API 的 Azure native 客户端。我已在 native 客户端和服务 API 上定义了应用程序角色,并将权限从 native 客户端分配给服务 API。这一切都很好,我确实获得了分配给服务 API 中定义的用户的应用程序角色。例如,我为 HTTP GET 和 HTTP POST 操作定义了 READ 和 WRITE 角色并保护了 API。但是,我没有获得在 native 客户端中定义的角色。我已经定义了 native 客户端角色,例如“用户”和“管理员”。这些角色将用于隐藏/显示 UI 的某些部分。当 native 客户端进行身份验证时,它指定 native 客户端 ID 和服务 API 的资源 ID。我知道我获得的 token 是专门用于该服务的,这可能就是为什么我只接收该服务的角色而不是我的客户端角色的原因。那么,如何在不使用图形 API 的情况下获得 native 客户端角色呢?身份验证方法需要资源ID。 native 客户端未定义资源ID(APP ID URI)。我希望同时接收客户端和资源应用程序角色,正如我在 token 请求中指定的那样。

 UserCredential uc = new UserCredential();
            try
            {
                // This method requires a resourceId. 
                // How do I request a token for just the client that returns client app roles, or client and servcie roles in one request?
                // Azure Native Client does not define a resourceId, a.k.a. APP ID URI
                result = authContext.AcquireTokenAsync(odataServiceResourceId, clientId, uc).Result; 

                var jwt = new JwtSecurityTokenHandler().ReadJwtToken(result.AccessToken);
                String[] roles = jwt.Claims.Where(c => c.Type == "roles").Select(c => c.Value).ToArray(); // missing client roles???
                txtResults.Text = "Application roles assigned to you: ";
                foreach(var role in roles)
                {
                    txtResults.Text += role + ",";
                }
                // roles is missing the client roles, but has the service roles???
                btnSignIn.Text = "Sign Out";
                lblUser.Text = String.Format(@"{0} {1}", result.UserInfo.GivenName, result.UserInfo.FamilyName);
                lblMessage.Text = "Sign in successfull";
            }
            catch (Exception ee)
            {
                lblMessage.Text = ee.Message;
                return;
            }

最佳答案

此身份验证仅需要访问资源的权限,因此资源 ID 是资源的 App ID url。所以我认为你的代码不会获得 native 应用程序的角色。如果你想做到这一点,我认为你需要图形 API。

关于.net - 如何获取Azure native 客户端应用程序角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640628/

相关文章:

c# - 有没有办法自动覆盖类上的 ToString() ?

azure - 获取自定义策略中的 Azure AD B2C 范围

redirect - 需要登录的网页应该重定向到登录页面还是只显示表单?

azure - 使用 Azure Functions(无需流分析)将数据从 Azure IoTHub 存储到 SQL 数据库中

azure - 查询以获取失败请求的百分比 Azure Application Insights

javascript - 从 javascript 设置常规 HTTP 请求 header

authentication - AIR 移动 Twitter OAuth 不返回 oauth_verifier

c# - 并发使用 Wea​​kReference<T>?

c# - 在 2D map 上查找满足多个条件的位置

c# - 动态postbackurl