WebApi 2 + Owin 在令牌请求上使用 urlencoded 正文

WebApi 2 + Owin Use urlencoded body on token request

在我的 WebAPI2 应用程序中,我通过 Owin 中间件使用 OAuth 身份验证。为了获得令牌,客户端应在请求中使用 application/x-www-form-urlencoded 正文。

 function userAccount($resource, appSettings) {
    return {
        registration: $resource(appSettings.serverPath + "/api/Account/Register", null, 
                {
                    'registerUser' : { method : 'POST'}
                }
            ),
        login : $resource(appSettings.serverPath + "/Token", null, 
                {
                    'loginUser': {
                        method: 'POST',
                        headers: {
                            'Content-Type' : 'application/x-www-form-urlencoded' 
                        },
                        transformRequest: function (data, headersGetter) {
                            var str = [];
                            for (var d in data) {
                                str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
                            }
                            return str.join("&"); 
                        }
                    }
                } 
            )
    }
}

但是有什么方法可以覆盖此行为以使用 json 格式的原始正文吗?而不是这个:"grant_type=password&username=user&password=123456" 想使用这个:“{ grant_type: "password", username:"user", password="123456" }".

感谢任何建议。

您可以在控制器中将操作设置为 "proxy" 方法,该方法可以在主体中接受 json,然后使用 url 编码参数调用内部方法。