2

Hey I just programmed a token based authentication following this tutorial. So everything goes fine as long I send my POST request as x-www-form-urlencoded. So now my teammate needs to get the token with a json, but all he gets is "unsupported grant_type". So can I change the acceptable type for the token or do I have to find another solution?

My configuration looks like this:

public void Configuration(IAppBuilder app)
    {
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        var myProvider = new MyAuthorizationServerProvider();
        OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = myProvider
        };

        app.UseOAuthAuthorizationServer(options);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);
        }
    }

This is how my request look like keep in mind this doesnt work with json This is how my request look like keep in mind this doesnt work with json And with a JSON it doesnt work: enter image description here Best regards :)

Johannes Gnadlinger
  • 1,339
  • 1
  • 12
  • 32

1 Answers1

3

The reason behind the use of application/x-www-form-urlencoded as Content-Type is simple: the OAuth2 specification (RFC 6749) requires this content type for token requests.

Any other content-type will break OAuth2 compliant clients compatibility. I advice you to not change this standard behavior.

The default implementation of OAuthAuthorizationServerMiddleware (more precisely the internally used OAuthAuthorizationServerHandler) from Microsoft.Owin.Security.OAuth just ignores the Content-Type header and tries to read the request body as a form anyway.

For Another way , In RequestBody you can write, grant_type=password&username=yourUserName&password=MyPassword123,

Also make sure after grant_type=password&username=username&password=password there is no space or line break.

enter image description here

Vivek Singh
  • 1,113
  • 10
  • 20