asp.net-web-api – 如何设置katana-project以允许json格式的令牌请求?

前端之家收集整理的这篇文章主要介绍了asp.net-web-api – 如何设置katana-project以允许json格式的令牌请求?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我在asp.net webapi 2中设置OAuth授权服务器时,如何设置令牌端点以接受json而不是表单编码的帖子?

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

我试着发送application / json

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

我收到了回复
400错误请求

{
  "error" : "unsupported_grant_type"
}

其中作为application / x-www-form-urlencoded的内容类型
和grant_type =密码&用户名= Alice&密码=密码123
按预期工作。

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

解决方法

基于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>

原文链接:https://www.f2er.com/aspnet/252002.html

猜你在找的asp.Net相关文章