我目前正在开发一个应用程序,以允许我公司的主管从任何地方执行某些操作。我的Node.js作为Internet和我公司内部局域网之间的代理,并调用公司局域网上的一些ASP.NET WEB API服务来处理Active Directory认证(因此执行人员可以使用他们的Windows登录)并存储和检索数据来自SQL Server 2008.将数据发布到Node.js的ASP.NET WEB API服务
ASP.Net AuthenticateUser()函数接受包含userId和pas的HTTP POST请求,并返回包含客户端可用于签名请求的哈希的JSON对象。当我将userId &密码作为查询参数传递时它工作正常,但当我尝试将它们嵌入到请求正文中时失败。工作版本如下所示。 (注:我省略了错误处理,以提高清晰度。)
namespace Approver.Controllers
{
public class UtilityController : ApiController
{
public UserInfo AuthenticateUser(string userId, string password)
{
return UtilityBo.AuthenticateUser(userId, password);
}
}
}
工作Node.js的代码(即快速路由)看起来是这样的。
exports.login = function(req, res){
var userId = req.body.userId;
var password = req.body.password;
var postData = 'userId='+userId+'&password='+password;
});
var options = {
host: res.app.settings['serviceHost'],
port: res.app.settings['servicePort'],
path: '/api/Utility/AuthenticateUser?userId='+userId+'&password='+password,
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
http.request(options, function(resp){
resp.setEncoding('utf8');
var arr = '';
resp.on('data', function (chunk){
arr += chunk;
});
resp.on('end', function(){
var data = JSON.parse(arr);
res.writeHead(200, {'Content-Type': 'application/json',});
res.end(JSON.stringify(data));
});
}).end(JSON.stringify(postData));
当我将代码更改为仅接受帖子正文中的数据时,IIS返回HTTP 500错误。新的WEB API服务如下所示。
namespace Approver.Controllers
{
public class UtilityController : ApiController
{
public UserInfo AuthenticateUser(UserAuthentication userAuth)
{
return UtilityBo.AuthenticateUser(userAuth);
}
}
}
namespace Approver.Models
{
public class UserAuthentication
{
[Required]
public string UserId { get; set; }
[Required]
public string Password { get; set; }
}
}
新的节点代码只是从URL中删除查询参数。
做了一些测试之后,我意识到web api从未将数据插入到模型中,所以userAuth始终具有空值。不过,我尝试通过Chrome的REST控制台发布数据,并且它工作正常,所以Node.js文件必须包含错误。我该如何解决?