2017-05-08 99 views
0

我正在使用在蔚蓝环境中部署的REST服务。我想通过从单独的(控制台)应用程序调用各种AP​​I函数来运行一些集成测试。但REST api使用不记名令牌认证。我是一个天真的认证总noob,所以我甚至不知道它应该是可能的。生成不记名令牌客户端C#

我试过使用发现的例子here,但没有运气。

在任何情况下,我有两个应用程序。一个是运行代码的控制台应用程序,另一个是Rest服务,我需要使用不记名令牌来访问API调用。我将称它们为ConsoleApp和RestService。

我运行的代码是如下:

HttpClient client = new HttpClient(); 
string tenantId = "<Azure tenant id>"; 
string tokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/token"; 
string resourceUrl = "<RestService app id url>"; 
string clientId = "<azure id of the ConsoleApp>"; 
string userName = "[email protected]"; 
string password = "somepassword"; 

string tokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/token"; 
var body = $"resource={resourceUrl}&client_id={clientId}&grant_type=password&username={userName}&password={password}"; 
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); 

var result=await client.PostAsync(tokenEndpoint, stringContent).ContinueWith<string>((response) => 
{ 
    return response.Result.Content.ReadAsStringAsync().Result; 
}); 

JObject jobject = JObject.Parse(结果);

JSON的消息,我回去:

error: invalid_grant, error_description: AADSTS50105: The signed in user is not assigned to a role for the application "RestService azureid"

这是什么意思,以及如何需要做得到的承载标记出这是什么?

回答

1

请先检查您是否启用控制台应用程序的User assignment required

在你湛蓝的广告片,点击Enterprise applications,搜索你的应用程序中All applications刀片,单击Propertiesenter image description here

如果打开的话,和您的帐户未在您的应用中分配访问权限,那么您将收到错误消息。请尽量在你的应用程序指定访问角色:

在你湛蓝的广告片,点击Enterprise applications,搜索你的控制台应用程序在All applications刀片,单击Users and groups,单击Add User按钮,选择您的帐户并分配角色(编辑用户,并确保select role是不是None Selected):

enter image description here

请让我知道它是否能帮助。

+0

控制台应用程序已关闭用户分配,服务已打开。我会看看我是否可以用这些信息做些什么 – martijn

+0

因此,我向服务添加了许可,并设法解决了我的问题。非常感谢! – martijn