Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpClient IsComplete始终返回false_C#_Asp.net_Asp.net Mvc_Asynchronous_Dotnet Httpclient - Fatal编程技术网

C# HttpClient IsComplete始终返回false

C# HttpClient IsComplete始终返回false,c#,asp.net,asp.net-mvc,asynchronous,dotnet-httpclient,C#,Asp.net,Asp.net Mvc,Asynchronous,Dotnet Httpclient,我正在尝试使用HttpClient-GetAsync方法授权用户从远程XMLWeb服务获取数据。不幸的是,不管服务器应答结果如何。IsCompleted总是在Controller中返回false。我做错了什么? 这是控制器: [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(CredentialsViewModel model)

我正在尝试使用HttpClient-GetAsync方法授权用户从远程XMLWeb服务获取数据。不幸的是,不管服务器应答结果如何。IsCompleted总是在Controller中返回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");
        }
若授权成功,这个存储库实际上必须返回布尔值

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;
        }
public async Task AuthenticateUser(字符串登录,字符串密码)
{
常量字符串url=@“http://somehost.ru:5555/api/getcountries";
var client=新的HttpClient();
var encoded=Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format(“{0}:{1}”,login,password));
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“基本”,编码);
var result=await client.GetAsync(url);
if(result.issucessStatusCode)返回true;
返回false;
}

您的控制器操作需要返回一个任务,因为所有异步方法都需要链接

帮助我记住:)

[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(CredentialsViewModel模型)
{
如果(!ModelState.IsValid)返回视图(“登录”);
var result=wait ar.AuthenticateUser(model.UserName,model.Password);
如果(!result.IsCompleted)
{
ModelState.AddModelError(“自定义错误”、“自定义错误”、“自定义错误”、“自定义错误”);
返回视图(“登录”);
}
FormsAuthentication.SetAuthCookie(model.UserName,false);
返回重定向到操作(“索引”、“主页”);
}

能否使用HTTP调试器(如Fiddler)查看请求/响应?它只是挂在“等待”处吗?
[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");
}