Хотя следующие статьи Azure Active Directory, я боролся со следующим кодом:Windows Forms Authentication с помощью 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);
Я получаю следующее сообщение об ошибке:
Error 1 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
c:\users\6025\documents\visual studio 2013\Projects\webapi.test\WindowsFormsApplication1\Form1.cs 43 28 WindowsFormsApplication1
Что я неправильно делаю?
Сообщение об ошибке довольно прямолинейно. Вам нужно изменить свой метод на 'async'. –
Можете ли вы опубликовать код всего вашего метода, включая подпись метода? –