mattbrailsford / umbraco-authu

An Umbraco plugin to add an OAuth API endpoint to allow authenticated Members/Users via OAuth
MIT License
71 stars 22 forks source link

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json #3

Closed txbrown closed 7 years ago

txbrown commented 7 years ago

@mattbrailsford I am opening this issue more as a help to solve this problem with AuthU within my side project. AuthU works fine when I dont have my webapi serializing to json but it does this when I change the default webapi serializer to use json and remove xml.

More specifically, this only occurs when we have the attribute decorator [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] or [JsonIgnore] in OAuthTokenResponse

I have tried following some stackoverflow solutions to similar issues like this but those didnt work for me.

Do you have an idea of what this might be? Thanks!

mattbrailsford commented 7 years ago

Why do you need to change it? Doesn't the default handle both XML and JSON depending on what your accepts header is?

With regards to the specific error though, I could do with more info. What is your code for registering your serializer? What solutions have you tried?

txbrown commented 7 years ago

@mattbrailsford I wanted all my output to be json so I removed it. I'll remove the config for this and use accept header and see what happens.

this is my webapi.config (somehow cant style whole block as code in github...):

public class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        //format api output to json and indent
        config.Formatters.Remove(config.Formatters.XmlFormatter);
        config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
}
txbrown commented 7 years ago

I removed my api config and I still get the issue. I guess this is an underlying issue within my app

mattbrailsford commented 7 years ago

And how are you calling WebApiConfig.Register in relation to the AuthU config?

txbrown commented 7 years ago

This is how I have my AppStart:

protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        WebApiConfig.Register(GlobalConfiguration.Configuration);

        OAuth.ConfigureEndpoint("/oauth/token", new OAuthOptions
        {
            UserService = new UmbracoMembersOAuthUserService(),
            SymmetricKey = "856FECBA3B06519C8DDDBC80BB080553",
            AccessTokenLifeTime = 20, // Minutes
            AllowInsecureHttp = true // During development only
        });
    }
ievgeniistepanenko commented 7 years ago

Hi. You can try update Newtonsoft.Json to 9.0.1 for Umbraco project. After change in web.config

bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"

for dependentAssembly "Newtonsoft.Json"

txbrown commented 7 years ago

@mattbrailsford gave another try to the package and followed @ievgeniistepanenko tip - it is now working. thanks! the issue can be closed.

mattbrailsford commented 7 years ago

@codenakama ahhh, of course. Glad you got it working.