【发布时间】:2017-10-06 20:16:16
【问题描述】:
我正在使用部署在天蓝色环境中的 REST 服务。我想通过从单独的(控制台)应用程序调用各种 API 函数来运行一些集成测试。但是 REST api 使用不记名令牌身份验证。我是一个具有 azure 身份验证的菜鸟,所以我什至不知道这是否应该可行。
我已尝试使用找到的示例 here,但还没有成功。
无论如何,我有两个应用程序。一个是运行代码的控制台应用程序,另一个是我需要使用不记名令牌访问 API 调用的 Rest 服务。我将它们称为 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 = "derp@flerp.onmicrosoft.com";
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(result);
我返回的 Json 消息:
错误:invalid_grant,error_description:AADSTS50105:已登录 用户未分配给应用程序“RestService”的角色 天蓝色”
这是什么意思,需要怎样做才能从中获得不记名令牌?
【问题讨论】:
标签: c# rest oauth azure-active-directory