Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

asp.net web api - How do you set katana-project to allow token requests in json format?

When I setup an OAuth Authorization Server in asp.net webapi 2 how can I set the token endpoint to accept json rather then a form encoded post?

Using the sample http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

I tried to send application/json with

{
"grant_type":"password",
"username":"Alice",
"password":"password123"
}

I receive the response 400 Bad Request

{
  "error" : "unsupported_grant_type"
}

where as a content type of application/x-www-form-urlencoded and body of grant_type=password&username=Alice&password=password123 works as expected.

200 OK

{
  "access_token" : "08cQ33ZG728AqBcj1PBsRSs4iBPc02lLCZfpaRRWLx2mH_wpQzMwGDKS7r7VgJiKUjUFaq6Xv0uguINoiB_evVbVOtvyWaqAYvc0HRjlgrbj12uQqFbUB7bgH-jiyfhumkwuTSTVHfKUhBjCuD_pbyxEbu2K5WSJpUVge_SGxnb-htm4ZNf1qKDmpEnP9IpZVeJa-KnV0m0gEmP04slMW_JrO390LzCNvXZwVk1yMNuvDakk8tWX7Y6WkFoh7vtW6xfhw3QMbmnvS6px70yMWcTksRNG2bdmi4SenhuRTJx8IsCMnz-4Co7KiCNJGF7KLeU4WzE-LSqXv3mQ30CIQ7faXoMn53p83wZ1NoXYyhsNrQD4POUns_Isb_Pax5gvpZEdyo8zr1r7wb0dS7UXvJb0PWzLHc57Pg3u0kmcizQ",
  "token_type" : "bearer",
  "expires_in" : 1209599,
  "userName" : "Alice",
  ".issued" : "Wed, 30 Oct 2013 15:16:33 GMT",
  ".expires" : "Wed, 13 Nov 2013 15:16:33 GMT"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Based on the current implementation of OAuthAuthorizationServerHandler you cannot.

private async Task InvokeTokenEndpointAsync()
{
     DateTimeOffset currentUtc = Options.SystemClock.UtcNow;
     // remove milliseconds in case they don't round-trip
     currentUtc = currentUtc.Subtract(TimeSpan.FromMilliseconds(currentUtc.Millisecond));

     IFormCollection form = await Request.ReadFormAsync();
     var clientContext = new OAuthValidateClientAuthenticationContext(
                Context,
                Options,
                form);

     await Options.Provider.ValidateClientAuthentication(clientContext);

     if (!clientContext.IsValidated)
     {
          _logger.WriteError("clientID is not valid.");
          if (!clientContext.HasError)
          {
               clientContext.SetError(Constants.Errors.InvalidClient);
          }
          await SendErrorAsJsonAsync(clientContext);
          return;
      }

      var tokenEndpointRequest = new TokenEndpointRequest(form);
}

So in order to attempt this you will need to provide your own implementation of OAuthAuthorizationServerMiddleware that overloads CreateHandler so you can provide your own implementation of AuthenticationHandler<OAuthAuthorizationServerOptions>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...