Asp.net MVC调用登录web服务

本文关键字:web 服务 登录 调用 net MVC Asp | 更新日期: 2023-09-27 18:16:20

我是Asp新手。我想做的是。我有一个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。净MVC。如果登录成功(Status:True)。我应该重定向到仪表板,并在我的视图页面显示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();
}

当我试图登录上面的代码。它总是显示"无效的用户名或密码"。所以提前感谢你的帮助。期待成功

Asp.net MVC调用登录web服务

我认为问题出在:

          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()响应,可能正在返回您提到的[{"UserID":0,"Status":"True","Name":"885032-59-6715","DepName":"Ajay"}]的JSON,这意味着测试responseData=="true" aka。"[{"UserID":0,"Status":"True","Name":"885032-59-6715","DepName":"Ajay"}]" == "true"结果为假。

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

我认为要走的路是,在你ReadAsStringAsync()之后,你应该通过JsonConvert.DeserializeObject<LoginResultModel>(responseData);将字符串(json)反序列化成一个对象。json转换在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);的列表

p。:你可以通过调试来查看responseData获得的数据,并理解为什么它的计算结果为false。