我正在使用在蔚蓝环境中部署的REST服务。我想通过从单独的(控制台)应用程序调用各种API函数来运行一些集成测试。但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"
这是什么意思,以及如何需要做得到的承载标记出这是什么?
控制台应用程序已关闭用户分配,服务已打开。我会看看我是否可以用这些信息做些什么 – martijn
因此,我向服务添加了许可,并设法解决了我的问题。非常感谢! – martijn