当我在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=password&username=Alice&password=password123
的正文
按预期工作。
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"
}
答案 0 :(得分:16)
根据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>
实现