0

I have a WebApi login function:

public HttpResponseMessage Login(MyUser user)
{
    Repository r = new Repository();

    user = r.Select<MyUser>(u => u.Login == user.Login && u.Password == user.Password).FirstOrDefault();
    if (user == null) 
    {
        // - No user found/wrong password, returning error
    }

    // - User found
    MyIdentity m = new MyIdentity(user);
    m.SignIn(); // - creates cookie, logins

    return Request.CreateResponse<Response>(HttpStatusCode.OK, new Response(true, "User logged in.", user));
}

The Response class is a generic response I use to deal with Ajax stuff and it accepts an object optionally:

public class Response
{
    public bool Status { get; set; }
    public string Message { get; set; }
    public object Data { get; set; }
}

Whenever I call the function, I get the expected JSON for the Response and any object attached, if any, on Internet Explorer, Edge, Opera and Chrome, but Firefox throws an exception of:

System.Runtime.Serialization.SerializationException: Type 'Serialization.Dog' with data contract name 'Dog:http://schemas.datacontract.org/2004/07/Serialization' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

and I end up getting a error 500 as result.

Why is that and what's the proper way to deal with this to work with all browsers?

Danicco
  • 1,573
  • 2
  • 23
  • 49
  • Where is `Dog` from? – Albireo Jan 19 '16 at 13:23
  • Firefox can't throw a SerializationException. Your code is throwing it possibly in response to something that Firefox is sending. Check the stack trace in the exception to find where in your code the Exception gets thrown. – Gabriel Luci Jan 19 '16 at 13:26
  • @Albireo Sorry, my OS is in another language so I copy/pasted the same error from elsewhere, `Dog` is supposed to be `MyUser` – Danicco Jan 19 '16 at 13:26
  • How is MyUser defined? You're serializing it when returning it to the client, is that class *and everything it uses, all the way down* serializable? – Albireo Jan 19 '16 at 13:30
  • Are you directly returning an Entity Framework entity? Does this help? http://stackoverflow.com/a/13077670 – Albireo Jan 19 '16 at 13:32
  • Can you show the call to this method? The error is pretty clear, what you are sending will not serialize into MyUser. Please show the call to your method and the MyUser class, you just have a type mismatch somewhere and it cannot serialize it. You also need to be specific as to where the content is coming from. Login([FromBody]MyUser user) and mark your method with [HttpPost] – Stephen Brickner Jan 19 '16 at 13:33

1 Answers1

0

In this case if it is working in all browsers except one then it is most likely that firefox is sending other data in the request. Be specific as to what data needs to be passed to your method ([FromBody]).

$.ajax({
        url: "http://yoururl.com",
        type: "POST",
        data: JSON.stringify({login: "yourLogin", password: "yourPassword"}),
        datatype: "json",
        contentType: "application/json; charset=utf-8",
        async: false
}).then(function (response) {
    //check response
});

[HttpPost]
public IHttpActionResult Login([FromBody]MyUser user)
{
    try
    {
        Repository r = new Repository();

        user = r.Select<MyUser>(u => u.Login == user.Login && u.Password ==     user.Password).FirstOrDefault();
        if (user == null) 
        {
            // - No user found/wrong password, returning error
            return Unauthorized();
        }

        // - User found
        MyIdentity m = new MyIdentity(user);
        m.SignIn(); // - creates cookie, logins

        return Ok(new Response(true, "User logged in.", user));
    }
    catch
    {
        return InternalServerError();
    }
}
Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19