You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.Net Core 2中使用Microsoft Graph API发送PATCH请求的正确方式

在ASP.NET Core 2中发送PATCH请求调用Azure AD Graph API的解决方案

我来帮你搞定这个问题!在ASP.NET Core 2里,HttpRequestMessage确实没有直接提供PATCH作为预设的HttpMethod枚举值,但咱们有两种简单可靠的方式来实现需求:

方法一:手动创建PATCH请求方法

HttpMethod类支持通过构造函数传入自定义的HTTP方法名称,所以我们可以直接实例化一个PATCH类型的请求方法,再传入HttpRequestMessage中:

using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;

// 替换为实际的用户ID和API版本
var targetUserId = "user-guid-here";
var apiEndpoint = $"https://graph.windows.net/myorganization/users/{targetUserId}?api-version=1.6";

// 创建PATCH类型的HttpMethod
var patchHttpMethod = new HttpMethod("PATCH");
var request = new HttpRequestMessage(patchHttpMethod, apiEndpoint);

// 构造要更新的用户属性(示例:更新显示名称)
var updatePayload = new 
{
    displayName = "Updated User Display Name"
};
// 将对象序列化为JSON,设置请求体格式
request.Content = new StringContent(
    JsonConvert.SerializeObject(updatePayload),
    Encoding.UTF8,
    "application/json"
);

// 添加身份验证头(替换为你的有效访问令牌)
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "your-access-token");

// 发送请求并处理响应
using (var httpClient = new HttpClient())
{
    var response = await httpClient.SendAsync(request);
    // 确保请求成功,若失败会抛出异常
    response.EnsureSuccessStatusCode();
}

方法二:封装HttpClient扩展方法

如果需要多次发送PATCH请求,咱们可以封装一个扩展方法,让调用更简洁:

using System.Net.Http;
using System.Threading.Tasks;

public static class HttpClientPatchExtensions
{
    public static Task<HttpResponseMessage> PatchAsync(
        this HttpClient client, 
        string requestUri, 
        HttpContent content)
    {
        var patchRequest = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri)
        {
            Content = content
        };
        return client.SendAsync(patchRequest);
    }
}

// 调用示例
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;

var targetUserId = "user-guid-here";
var apiEndpoint = $"https://graph.windows.net/myorganization/users/{targetUserId}?api-version=1.6";

var updatePayload = new 
{
    displayName = "Updated User Display Name"
};
var requestContent = new StringContent(
    JsonConvert.SerializeObject(updatePayload),
    Encoding.UTF8,
    "application/json"
);

using (var httpClient = new HttpClient())
{
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "your-access-token");
    var response = await httpClient.PatchAsync(apiEndpoint, requestContent);
    response.EnsureSuccessStatusCode();
}

几个重要注意事项

  • API版本:务必指定正确的Azure AD Graph API版本(示例中用的是1.6,这是旧API的最新版本),另外建议优先考虑迁移到Microsoft Graph API(https://graph.microsoft.com),因为Azure AD Graph已进入退役阶段。
  • 权限配置:你的访问令牌需要拥有足够的权限(比如Directory.AccessAsUser.All或特定的用户更新权限),要在Azure AD应用注册中提前配置并同意这些权限。
  • 请求体格式:PATCH请求只需要传入需要更新的属性,不需要提交完整的用户对象,且必须是JSON格式。

内容的提问来源于stack exchange,提问作者Daarwin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:19:54