【问题标题】:Azure graph API / c# patch URLAzure 图形 API/c# 补丁 URL
【发布时间】:2018-04-02 18:47:49
【问题描述】:

我正在尝试编写密码重置应用程序 c# 和图形 API。我已在 Azure 中为应用程序设置权限,收到有效令牌,并且可以请求信息。

我在尝试执行重置时收到 400 Bad Request 响应。我相信我形成的 URL 不正确。这是我收到的回复,后面是我的代码。

提前致谢!

Response: StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  request-id: omitted
  client-request-id: omitted
  x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Central US","Slice":"SliceA","Ring":"3","ScaleUnit":"002","Host":"AGSFE_IN_29","ADSiteName":"CHI"}}
  Duration: 43.0949
  Strict-Transport-Security: max-age=31536000
  Cache-Control: private
  Date: Mon, 02 Apr 2018 18:06:06 GMT
  Content-Type: application/json
}

private static async Task ResetPasswordAsync(HttpClient client, string UPN)
    {
        var payload = new
        {
            accountEnabled = true,
            passwordProfile = new
            {
                forceChangePasswordNextSignIn = true,
                password = "Password!"
            }
        };
        var payloadJSON = JsonConvert.SerializeObject(payload);
        Console.WriteLine(payloadJSON);
        HttpMethod method = new HttpMethod("PATCH");
        string requestUrl = $"https://graph.microsoft.com/v1.0/users/{UPN}?api-version=1.6";
        var request = new HttpRequestMessage(method, requestUrl)
        {
            Content = new StringContent($"{payloadJSON}", Encoding.UTF8, "application/json")
        };
        var response = await client.SendAsync(request);
        Console.WriteLine("Response: " + response);
        if (!response.IsSuccessStatusCode)
        {
            throw new InvalidOperationException(response.ReasonPhrase);
        }
    }

【问题讨论】:

    标签: c# azure graph passwords


    【解决方案1】:

    根据400 Bad Request,我们可以知道http请求有问题。在您的情况下,您可以使用 Fiddler 来捕获 htt 请求,我们可以获得Query parameter api-version not allowed。所以你可以从 requesturl 中删除 api 版本。

    我们可以从 Graph Update user API 获得更多关于更新使用的信息

    PATCH /users/{id | userPrincipalName}
    

    我们还需要在请求头中添加授权。

    string requestUrl = $"https://graph.microsoft.com/v1.0/users/{UPN}";
    var token ="Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFCSGg0...."
    ...
    request.Headers.Add("Authorization", token);
    var response = await client.SendAsync(request);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 2018-04-24
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      相关资源
      最近更新 更多