生成不记名令牌客户端 C#
Generate bearer token client side C#
我正在使用部署在 Azure 环境中的 REST 服务。我想通过从单独的(控制台)应用程序调用各种 API 函数来 运行 一些集成测试。但是 REST api 使用不记名令牌身份验证。我是天蓝色认证的菜鸟,所以我什至不知道它是否可行。
我尝试使用找到的示例 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(结果);
我收到的 Json 消息:
error: invalid_grant, error_description: AADSTS50105: The signed in
user is not assigned to a role for the application "RestService
azureid"
这是什么意思,如何从中获取不记名令牌?
请首先检查您是否启用了控制台应用程序的 User assignment required
:
在您的 azure ad blade 中,点击 Enterprise applications
,在 All applications
blade 中搜索您的应用,点击 Properties
:
如果启用该功能,并且您的帐户未在您的应用中分配访问角色,那么您将收到错误消息。请尝试在您的应用中分配访问角色:
在您的 azure ad blade 中,单击 Enterprise applications
,在 All applications
blade 中搜索您的控制台应用程序,单击 Users and groups
,单击 Add User
按钮,select 您的帐户并分配角色(编辑用户并确保 select role
不是 None Selected
):
请告诉我是否有帮助。
我正在使用部署在 Azure 环境中的 REST 服务。我想通过从单独的(控制台)应用程序调用各种 API 函数来 运行 一些集成测试。但是 REST api 使用不记名令牌身份验证。我是天蓝色认证的菜鸟,所以我什至不知道它是否可行。
我尝试使用找到的示例 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(结果);
我收到的 Json 消息:
error: invalid_grant, error_description: AADSTS50105: The signed in user is not assigned to a role for the application "RestService azureid"
这是什么意思,如何从中获取不记名令牌?
请首先检查您是否启用了控制台应用程序的 User assignment required
:
在您的 azure ad blade 中,点击 Enterprise applications
,在 All applications
blade 中搜索您的应用,点击 Properties
:
如果启用该功能,并且您的帐户未在您的应用中分配访问角色,那么您将收到错误消息。请尝试在您的应用中分配访问角色:
在您的 azure ad blade 中,单击 Enterprise applications
,在 All applications
blade 中搜索您的控制台应用程序,单击 Users and groups
,单击 Add User
按钮,select 您的帐户并分配角色(编辑用户并确保 select role
不是 None Selected
):
请告诉我是否有帮助。