'System.Net.Http.HttpRequestException' 发生在 mscorlib.dll 但未在用户代码中处理
'System.Net.Http.HttpRequestException' occurred in mscorlib.dll but was not handled in user code
我们正在开发 windows 表单应用程序以对 Azure AD 的 windows 表单应用程序进行身份验证。
我有几个问题,网上的资源似乎都过时了。
按照下面的建议 link 我已经创建了一个网络 api 和本机客户端 api 和 windows 表单应用程序。
在网络中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(请求);
类型 'System.Net.Http.HttpRequestException' 的异常发生在 mscorlib.dll 但未在用户代码中处理
附加信息:发送请求时出错。
我运行你的代码在正确设置这些变量后,起初我得到了同样的错误。然后我在httpRequestMessage request =new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/")
.
中更改我的请求url
现在它 运行 成功了,请确保您的 url https://localhost:***/task/api/
正确,仔细检查 localhost:***
.
中的端口
请阅读一些教程以获取来自 here 的更多信息。
我们正在开发 windows 表单应用程序以对 Azure AD 的 windows 表单应用程序进行身份验证。
我有几个问题,网上的资源似乎都过时了。
按照下面的建议 link 我已经创建了一个网络 api 和本机客户端 api 和 windows 表单应用程序。
在网络中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(请求);
类型 'System.Net.Http.HttpRequestException' 的异常发生在 mscorlib.dll 但未在用户代码中处理
附加信息:发送请求时出错。
我运行你的代码在正确设置这些变量后,起初我得到了同样的错误。然后我在httpRequestMessage request =new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/")
.
现在它 运行 成功了,请确保您的 url https://localhost:***/task/api/
正确,仔细检查 localhost:***
.
请阅读一些教程以获取来自 here 的更多信息。