【问题标题】:How to get Azure Native Client Application Roles?如何获取 Azure 本机客户端应用程序角色?
【发布时间】:2016-12-03 02:20:11
【问题描述】:

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

 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;
            }

【问题讨论】:

    标签: .net azure authentication adal role


    【解决方案1】:

    这个认证只需要访问资源的权限,所以资源ID就是资源的App ID url。所以我认为你的代码不会得到本机应用程序的作用。如果你想这样做,我认为你需要图形 API。

    【讨论】:

    • 这也是我得出的结论。我必须向图形 API 授予权限,然后调用以获取我的客户端的应用程序角色。
    猜你喜欢
    • 2013-08-21
    • 2020-08-25
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2020-09-19
    • 2019-06-16
    • 2018-01-07
    • 2018-02-28
    相关资源
    最近更新 更多