【问题标题】:'System.Net.Http.HttpRequestException' occurred in mscorlib.dll but was not handled in user code“System.Net.Http.HttpRequestException”发生在 mscorlib.dll 中,但未在用户代码中处理
【发布时间】:2016-07-31 06:47:41
【问题描述】:

我们正在开发 Windows 窗体应用程序以向 Azure AD 验证 Windows 窗体应用程序。

我有几个问题,网上的资源似乎都过时了。

按照以下链接的建议,我创建了一个 webapi 和本机客户端 api 和 windows 窗体应用程序。

在 web api 中,我更新了清单文件如下

“oauth2Permissions”:[

{

  "adminConsentDescription": "Allow the application to access ftpwebapi on behalf of the signed-in user.",

  "adminConsentDisplayName": "Access ftpwebapi",

  "id": "4eebeb18-aee6-408a-bea1-c090766dce23",

  "isEnabled": true,

  "origin": "Application",

  "type": "User",

  "userConsentDescription": "Allow the application to access ftpwebapi on your behalf.",

  "userConsentDisplayName": "Access ftpwebapi",

  "value": "user_impersonation"

}

],

并在keys下添加了我已经添加了2年的条目并将webapi添加到本机客户端应用程序中。

在 windows 窗体中,我在按钮单击事件中添加了以下代码。

private async void button1_Click(object sender, EventArgs e)

    {

        string authority = "https://login.windows.net/****";

        string resourceURI = "https://***.com/WebApplication3";

        string clientID = "********-5815-4358-****-*********";

        Uri returnURI = new Uri("http://********.com");

        AuthenticationContext authContext =

            new AuthenticationContext(authority);

        AuthenticationResult authResult =

            authContext.AcquireToken(resourceURI, clientID, returnURI);

        string authHeader = authResult.CreateAuthorizationHeader();

        // don't do this in prod

        System.Net.ServicePointManager.ServerCertificateValidationCallback =

            ((s, c, c2, se) => true);

        HttpClient client = new HttpClient();

        HttpRequestMessage request =

            new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/");

        request.Headers.TryAddWithoutValidation("Authorization", authHeader);

        var response = await client.SendAsync(request);

        string responseString = await response.Content.ReadAsStringAsync();

        MessageBox.Show(responseString);

}

到目前为止是正确的方式吗?

我正在处理一个错误

var response = await client.SendAsync(request);

在 mscorlib.dll 中发生了“System.Net.Http.HttpRequestException”类型的异常,但未在用户代码中处理

附加信息:发送请求时出错。

【问题讨论】:

  • 将 try/catch 添加到 sendAsync 并将您的异常内容发送给我:try { var response = await client.SendAsync(request); } catch (HttpRequestException e) { Console.WriteLine(e.InnerException.Message); }

标签: c# azure httpclient


【解决方案1】:

我在正确设置这些变量后运行您的代码,起初我得到了同样的错误。然后我在httpRequestMessage request =new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/")中更改我的requesturl。

现在它运行成功了,确保你的 url https://localhost:***/task/api/ 是正确的,仔细检查你在localhost:*** 中的端口。

请阅读一些教程以从here 获取更多信息。

【讨论】:

  • 感谢 Lily,我已将 requesturl 更改为从 azure 重定向我的本机客户端应用程序的 URI。它奏效了
  • 嗨 Rinu,你为什么叫原生应用?我不认为本机应用程序可以用作 api 吗?我使用你的代码实现了通过原生应用调用webapi,所以我将webapi url设置为requesturl并将原生应用id设置为clientID
  • 本地应用程序,我的意思是本地客户端应用程序(它具有来自 webapi 的权限),我在上面的 uri 中使用本地客户端应用程序的重定向 URI,它的工作原理类似于 global.com/task/api no端口号在那里并且它工作了
  • 是的,但是我可以看到您在本机客户端应用程序中设置了oauth2Permissions,我以为您想通过本机客户端应用程序访问 webapi,不是吗?我只是想知道你的目的地使用上面private async void button1_Click(object sender, EventArgs e)下的代码。
猜你喜欢
  • 2014-01-07
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多