【发布时间】:2016-03-16 22:01:07
【问题描述】:
在阅读文章Azure Active Directory 时,我遇到了以下代码:
string authority = "https://login.windows.net/winsmartstest.onmicrosoft.com";
string resourceURI = "https://winsmartstest.onmicrosoft.com/MyWebAPI";
string clientID = "9329c7a4-2d61-467b-94b8-c5ce67cca6c3";
Uri returnURI = new Uri("http://doesntreallymatter");
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:44300/api/tasks");
request.Headers.TryAddWithoutValidation("Authorization", authHeader);
var response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseString);
我收到以下错误:
错误 1 'await' 运算符只能在异步方法中使用。考虑使用“异步”修饰符标记此方法并将其返回类型更改为“任务”。
c:\users\6025\documents\visual studio 2013\Projects\webapi.test\WindowsFormsApplication1\Form1.cs 43 28 WindowsFormsApplication1
我做错了什么?
【问题讨论】:
-
错误信息非常简单。您需要将方法更改为
async。 -
你能贴出你的整个方法的代码,包括方法签名吗?
标签: azure async-await office365 azure-active-directory