我想通过 C# REST 调用生成 Azure AD 不记名令牌。我想在 API 调用中编写用户注册逻辑。我将其用作令牌端点:
https://login.windows.net/[tenant-id]/oauth2/token
我遵循了与本文中描述的相同的程序。
我使用的用户凭据是“全局管理员”。
但我仍然收到“未经授权”错误。请在下面找到代码片段和响应正文 -
代码
using (HttpClient client = new HttpClient())
{
var tokenEndpoint = @"https://login.windows.net/<tanent-name>/oauth2/token";
var accept = "application/json";
client.DefaultRequestHeaders.Add("Accept", accept);
string postBody = @"resource=https%3A%2F%2Fgraph.microsoft.com%2F
&client_id=<client-id>
&grant_type=password
&username=<admin-user-name>
&password=<admin-pass>
&scope=openid";
using (var response = await client.PostAsync(tokenEndpoint, new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded")))
{
if (response.IsSuccessStatusCode)
{
var jsonresult = JObject.Parse(await response.Content.ReadAsStringAsync());
var token = (string)jsonresult["access_token"];
return token;
}
else
return null;
}
}
响应体
{ StatusCode: 401, ReasonPhrase: 'Unauthorized',
Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
x-ms-request-id: cabefe46-ff73-4659-80a2-2f4136200900
Cache-Control: no-store, no-cache
P3P: CP="DSP CUR OTPi IND OTRi ONL FIN"
Set-Cookie: esctx=AQABAAAAAADX8GCi6Js6SK82TsD2Pb7...
Set-Cookie: x-ms-gateway-slice=004; path=/; secure; HttpOnly
Set-Cookie: stsservicecookie=ests; path=/; secure; HttpOnly
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Thu, 24 May 2018 13:43:39 GMT
Content-Length: 457
Content-Type: application/json; charset=utf-8
Expires: -1
}}
PS-另外请指出是否有任何其他方式可以在不进行 REST 调用的情况下添加新用户。我不想在客户端应用程序中进行用户注册。