我有一个Azure功能应用程序与容易8月启用。我希望获得一个访问令牌来使用登录用户的权限访问power bi,这样用户就可以直接从前端访问power bi。
我已经在Azure AD上注册了一个具有power bi权限的应用程序,并且我创建了一个HTTP触发端点来将访问令牌返回到前端。
函数应用程序中没有任何用户名或密码,只有用户的ID令牌。是否有任何方式可以要求访问令牌代表用户使用ID令牌或UserAssertion访问power bi?
我的实施是在这里:
var context = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize");
var res = await context.AcquireTokenAsync("https://analysis.windows.net/powerbi/api",
clientId, new Microsoft.IdentityModel.Clients.ActiveDirectory.UserAssertion(idToken)).ConfigureAwait(false);
return new OkObjectResult(new { Token = res.AccessToken });但是当我试图运行这段代码时,它会抛出这个异常。
System.Private.CoreLib: Exception while executing function: GetPowerBiToken. Microsoft.IdentityModel.Clients.ActiveDirectory: AADSTS50027: JWT token is invalid or malformed.
这是得到令牌的正确方式吗?
发布于 2020-01-21 04:50:05
根据我的理解,您有一个公共客户端来登录用户。这个客户端将调用Azure函数应用程序上的API,您的函数应用程序将调用PowerBi来代表登录到您的公共客户端的用户执行一些任务。
在整个过程中,您应该注册两个Azure AD应用程序:
1.作为公共客户端的应用程序,您应该授予它访问Azure函数应用程序的授权,这样用户就可以登录并获得访问令牌来调用您的函数应用程序。
2. Azure函数应用程序,您应该授予它与powerbi相关的授权,以便它可以代表用户调用powerbi app。
因此,请按照以下步骤完成此过程:
1.在客户端,登录用户并获取请求资源的访问令牌,这是您的功能应用程序。
2.使用该访问令牌调用您的函数应用程序,以便您的函数应用程序可以使用代表流动获取访问令牌来调用powerbi资源。我在一个简单的控制台应用程序中测试了这个过程,它非常适合我,代码就是您想要的:
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AzureADUserAssertion
{
class Program
{
static void Main(string[] args)
{
var functionClientId = "<function azure ad app id>";
var functionSecret = "<function azure ad app secret>";
var functionCred = new ClientCredential(functionClientId, functionSecret);
var access_token_from_client_side = "<access token value>";
var context = new AuthenticationContext("https://login.windows.net/common/");
var res = context.AcquireTokenAsync("https://analysis.windows.net/powerbi/api",
functionCred, new Microsoft.IdentityModel.Clients.ActiveDirectory.UserAssertion(access_token_from_client_side, "urn:ietf:params:oauth:grant-type:jwt-bearer")).ConfigureAwait(false).GetAwaiter().GetResult();
Console.WriteLine( res.AccessToken );
Console.ReadKey();
}
}
}结果:

请检查此令牌:

希望能帮上忙。
https://stackoverflow.com/questions/59823445
复制相似问题