【问题标题】:WebApi 2 + Owin Use urlencoded body on token requestWebApi 2 + Owin 在令牌请求中使用 urlencoded 正文
【发布时间】:2016-01-12 10:12:26
【问题描述】:

在我的 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”}”。

欣赏任何建议。

【问题讨论】:

    标签: oauth asp.net-web-api2 owin


    【解决方案1】:

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

    【讨论】:

    • @sonicbils 谢谢你的回答。舒尔这可以解决问题,但认为这仍然是一种解决方法。
    • 是的,但我很确定,除非您自己实现,否则它是您唯一的选择。 stackoverflow.com/questions/19645171/….
    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 2018-11-26
    • 2015-07-02
    • 2016-10-03
    • 2021-09-02
    • 2014-11-05
    • 2021-12-03
    • 2015-04-23
    相关资源
    最近更新 更多