HttpClient IsComplete 总是 return false
HttpClient IsComplete always return false
我正在尝试授权用户使用 HttpClient GetAsync 方法从远程 xml Web 服务获取数据。不幸的是,无论服务器在 Controller 中回答 result.IsCompleted 总是 return false。我做错了什么?
这是控制器:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(CredentialsViewModel model)
{
if (!ModelState.IsValid) return View("Login");
var result = ar.AuthenticateUser(model.UserName, model.Password);
if (!result.IsCompleted)
{
ModelState.AddModelError("CustomError", "Вход в систему с указанными логином и паролем невозможен");
return View("Login");
}
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}
如果授权成功,这是实际上必须 return 布尔值的存储库。
public async Task<bool> AuthenticateUser(string login, string password)
{
const string url = @"http://somehost.ru:5555/api/getcountries";
var client = new HttpClient();
var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", login, password)));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
var result = await client.GetAsync(url);
if (result.IsSuccessStatusCode) return true;
return false;
}
您的控制器操作需要 return 一个任务,因为所有异步方法都需要链接起来。
Turtles all the way down 帮我记起来:)
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(CredentialsViewModel model)
{
if (!ModelState.IsValid) return View("Login");
var result = await ar.AuthenticateUser(model.UserName, model.Password);
if (!result.IsCompleted)
{
ModelState.AddModelError("CustomError", "Вход в систему с указанными логином и паролем невозможен");
return View("Login");
}
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}
我正在尝试授权用户使用 HttpClient GetAsync 方法从远程 xml Web 服务获取数据。不幸的是,无论服务器在 Controller 中回答 result.IsCompleted 总是 return false。我做错了什么?
这是控制器:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(CredentialsViewModel model)
{
if (!ModelState.IsValid) return View("Login");
var result = ar.AuthenticateUser(model.UserName, model.Password);
if (!result.IsCompleted)
{
ModelState.AddModelError("CustomError", "Вход в систему с указанными логином и паролем невозможен");
return View("Login");
}
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}
如果授权成功,这是实际上必须 return 布尔值的存储库。
public async Task<bool> AuthenticateUser(string login, string password)
{
const string url = @"http://somehost.ru:5555/api/getcountries";
var client = new HttpClient();
var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", login, password)));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
var result = await client.GetAsync(url);
if (result.IsSuccessStatusCode) return true;
return false;
}
您的控制器操作需要 return 一个任务,因为所有异步方法都需要链接起来。
Turtles all the way down 帮我记起来:)
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(CredentialsViewModel model)
{
if (!ModelState.IsValid) return View("Login");
var result = await ar.AuthenticateUser(model.UserName, model.Password);
if (!result.IsCompleted)
{
ModelState.AddModelError("CustomError", "Вход в систему с указанными логином и паролем невозможен");
return View("Login");
}
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}