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

部署到IIS的API调用MSGraph超时,开发环境与Postman可正常访问

排查解决方向
  • 代理配置差异排查

企业内网环境通常需要配置代理才能访问公网,Postman运行时会继承当前登录Windows用户的系统代理配置,但IIS应用池默认使用内置虚拟账户(如ApplicationPoolIdentity、Network Service),不会自动加载用户级的代理设置,这是此类问题最常见的诱因。
解决方法1:在应用的web.config文件中添加全局代理配置:

<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true" enabled="true">
      <proxy proxyaddress="http://你的代理服务器地址:端口" bypassonlocal="True" />
    </defaultProxy>
  </system.net>
</configuration>

解决方法2:给RestClient显式指定代理配置,避免依赖全局设置。

  • 应用池身份验证排查

临时将IIS应用程序池的运行身份修改为你当前登录服务器的Windows账号(即运行Postman测试正常的账号),如果修改后请求可正常返回,即可确认是运行身份对应的网络权限/代理配置问题。

  • 代码逻辑优化

    1. 避免每次请求都新建RestClient实例:重复实例化会导致套接字资源耗尽,高并发场景下容易触发连接失败,建议将RestClient注册为单例依赖注入复用。
    2. 替换同步阻塞调用:ExecuteAsync(request).Result属于同步阻塞异步方法的写法,容易引发线程池阻塞导致假超时,建议将接口改为异步写法,用await调用异步方法:
    [HttpGet]
    [Route("GetUserTest")]
    public async Task<IActionResult> GetUserTest(string userPrincipalName)
    {
        // 注意:RestClient建议改为单例注入,不要每次请求新建
        var client = new RestClient($"https://graph.microsoft.com/v1.0/users?$count=true&$search=\"userPrincipalName:{userPrincipalName}\"");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        request.AddHeader("authorization", "Bearer " + MSGraphTokenRequest());
        request.AddHeader("ConsistencyLevel", "eventual");
        IRestResponse response = await client.ExecuteAsync(request);
        if ((int)response.StatusCode != 200)
        {
            return Problem(response.ErrorMessage);
        }
        return Json(response.Content);
    }
    
  • 网络连通性验证

新增测试接口,在接口内请求其他公网HTTPS接口,确认是否所有公网请求都超时,进一步缩小问题范围。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 18:06:01