27

当我在 asp.net webapi 2 中设置 OAuth 授权服务器时,如何将令牌端点设置为接受 json 而不是表单编码的帖子?

使用示例http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

我试图发送应用程序/json

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

我收到响应 400 Bad Request

{
  "error" : "unsupported_grant_type"
}

其中作为 application/x-www-form-urlencoded 的内容类型和grant_type=password&username=Alice&password=password123 预期的作品主体。

200 好

{
  "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"
}
4

1 回答 1

18

根据您当前的实施情况,OAuthAuthorizationServerHandler您不能。

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);
}

所以为了尝试这个,你需要提供你自己的OAuthAuthorizationServerMiddleware重载实现,CreateHandler这样你就可以提供你自己的实现AuthenticationHandler<OAuthAuthorizationServerOptions>

于 2013-10-30T18:12:35.523 回答