2

I'm creating a login form using a MVC model. Actually, my application can GET, DELETE and even POST (Create) correctly. When I tried to use a POST method to send a JSON object in my Login Method, a "Bad Request" exception is thrown.

Here is some code to clear up my question: In the class LoginController, I implemented the login method as follows:

[HttpPost]
public ActionResult Login(LoginViewModel lvm)
{
  LoginServiceClient lsc = new LoginServiceClient();
  bool userFound_flag = lsc.Login(lvm.User);
  if (userFound_flag)
    return RedirectToAction("Index_User", lvm.User);
  else
    return RedirectToAction("Create");
  }
}

In the Model folder, this class LoginServiceClient is where I implement the Login method which will upload the JSON object as follows:

public bool Login(User user)
{
  try
  {
    var webClient = new WebClient();
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(User));
    MemoryStream mem = new MemoryStream();
    ser.WriteObject(mem, user);
    string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
    webClient.Headers["Content-type"] = "application/json";
    webClient.Encoding = Encoding.UTF8;
    String str = webClient.UploadString(BASE_URL + "login", "POST", data);
    return true;
  }
  catch (WebException webEx)
  {
    return false;
  }
}

As you can see, the line String str = webClient.UploadString(BASE_URL + "login", "POST", data); is where the exception is thrown. This function is the same as the Create function which I use to create a new object in my Database. The string data is for example "\"City\":null,\"Country\":null,\"Email\":null,\"FirstName\":null,\"Gender\":null,\"Id\":0,\"LastName\":null,\"Password\":\"EEPass\",\"Telephone\":0,\"UserName\":\"EE\"}"

On my Server Side, here is the Login method. The following method is in the IloginService.cs class:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "login", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
User Login(User _user);

And in the LoginSerive.svs.cs class, where I connect to my DB:

public User Login(User _user)
{
  using (OnlineStoreDBEntities ose = new OnlineStoreDBEntities())
  {
    return ose.UserEntities.Where(pe => pe.Username == _user.UserName && pe.Password == _user.Password).Select(pe => new User
    {
      Id = pe.Id,
      Email = pe.Email,
      FirstName = pe.FirstName,
      LastName = pe.LastName,
      //Telephone = Convert.ToInt32(pe.Telephone),
      Country = pe.Country,
      City = pe.City,
      //Gender = Convert.ToInt32(pe.Gender)
    }).First();
  };
}

Finally, to make sure I was clear, both the Create and Login functions have the same body in the Model class. I'm also using the POST method because some data should be hidden such as the password. But the upload string fails to upload the data (JSON string). When I display the data string it contains the data I need, so it's not empty.

Fraser Crosbie
  • 1,672
  • 1
  • 12
  • 21
BDeveloper
  • 1,175
  • 6
  • 24
  • 44

1 Answers1

0

The UploadString is wrapping the JSON in quotes since it is treating it as a string. So when the data gets to the login controller the controller is looking to deserialize a model from the body which consists of a string instead of a json object. Since it can not deserialize the User model from a single string, it fails.

The easist fix would to be to use one of the other WebClient upload methods like UploadData.

swestner
  • 1,881
  • 15
  • 19