首页 文章

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

提问于
浏览
0

在我的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“,用户名:”user“,密码=”123456“}” .

感谢任何建议 .

1 回答

  • 1

    您可以在控制器中将操作设置为“代理”方法,该方法可以接受正文中的json,然后使用url编码的参数调用内部方法 .

相关问题