Asp.net MVC呼叫登录网络服务

时间:2016-11-05 03:16:45

标签: c# asp.net-mvc asp.net-web-api

我是Asp.Net的新手,我想做的是。我有一个用于登录服务的Web API,它返回一个json数据。 Web APi的示例网址

http://localhost:55500/api/Login/submit?username=abc&password=abc123

它返回一个像

这样的json数据
[{"UserID":0,
  "Status":"True",
  "Name":"885032-59-6715",
  "DepName":"Ajay"} 
]

如何在Asp.NET MVC中验证我的登录页面。如果登录成功(状态:True)。我应该重定向到仪表板并在我的视图页面中显示json数据。    如果登录失败,则应显示错误消息

我的ASP.NET MVC模型calss文件:

namespace LoginPracticeApplication.Models{
  public class Login {

    [Required(ErrorMessage = "Username is required")] // make the field required
    [Display(Name = "username")]  // Set the display name of the field
    public string username { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [Display(Name = "password")]
    public string password { get; set; }       
  }}

我的ASP.NET MVC控制器文件:

public ActionResult Index(Login login)
{
  if (ModelState.IsValid) // Check the model state for any validation errors
  {
      string uname = "";
      uname = login.username;
      string pword = "";
      pword = login.password;

      string url = "http://localhost:55506/api/Login/submit?username=" + uname + "&password=" + login.password + "";
      System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
      client.BaseAddress = new Uri(url);
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
      HttpResponseMessage responseMessage = client.GetAsync(url).Result;

      var responseData = responseMessage.Content.ReadAsStringAsync().Result;
      if (responseData=="true")
      {                    
          return View("Show", login); // Return the "Show.cshtml" view if user is valid
      }
      else
      {
          ViewBag.Message = "Invalid Username or Password";
          return View(); //return the same view with message "Invalid Username or Password"
      }
  }
  else
  {
      return View();
  }
  return View();
}

当我尝试使用上面的代码登录时。它始终显示"无效的用户名或密码"。所以,提前感谢您的帮助。期待成功

1 个答案:

答案 0 :(得分:1)

我认为问题出在:

          var responseData = responseMessage.Content.ReadAsStringAsync().Result;
          if (responseData=="true")
          {                    
              return View("Show", login); // Return the "Show.cshtml" view if user is valid
          }
          else
          {
              ViewBag.Message = "Invalid Username or Password";
              return View(); //return the same view with message "Invalid Username or Password"
          }

当您ReadAsStringAsync()响应时,可能正在返回您提到的JSON [{"UserID":0,"Status":"True","Name":"885032-59-6715","DepName":"Ajay"}],这意味着测试responseData=="true"又名。 "[{"UserID":0,"Status":"True","Name":"885032-59-6715","DepName":"Ajay"}]" == "true"会导致错误。

你可以使用responseData.Contains("true"),但我不相信这是最好的方法。

我认为要走的路是,在你ReadAsStringAsync()之后,你应该通过JsonConvert.DeserializeObject<LoginResultModel>(responseData);将字符串(json)反序列化为一个对象。 JsonConvert在Newtonsoft.Json中,你可以通过Nuget获得。在LoginResultModel中,您应该考虑您的json。我相信它会是这样的:

public class LoginResultModel
{
    public int UserID { get; set; }

    public bool Status { get; set; }

    public string Name { get; set; }

    public string DepName { get; set; }
}

当你返回一个数组时,你应该反序列化为LoginResultModel列表:JsonConvert.DeserializeObject<List<LoginResultModel>>(responseData);

PS。:您可以通过调试来查看responseData获取的数据,并了解其评估为false的原因。

此致

相关问题