Asp.net MVC调用登录Web服务

穆罕默德(Mohamed Muzammil Irfan)

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

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

它返回一个json数据,例如

[{"UserID":0,
  "Status":"True",
  "Name":"885032-59-6715",
  "DepName":"Ajay"} 
]

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

我的ASP.NET MVC模型校准文件:

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();
}

当我尝试使用上面的代码登录时。它始终显示“无效的用户名或密码”。因此,在此先感谢您的帮助。期待成功

告诉我

我认为问题出在:

          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()您应该通过之后将字符串(json)反序列化为对象JsonConvert.DeserializeObject<LoginResultModel>(responseData);JsonConvert在Newtonsoft中。您可以通过Nuget获得Json。在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的原因。

问候

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章