C# 如何在WCF服务中使用查询字符串?
我正在尝试创建web服务,到目前为止它们工作得很好,但我有一个问题:如果我的查询字符串的顺序与我指定的顺序不一致,那么代码会给我错误的结果 在开始一个大型项目之前,我希望能够传入一个查询字符串,这样顺序就无关紧要了-传入“?user=foo&pass=bar”应该相当于“?pass=bar&user=foo”,但我不确定如何让它按预期工作 事实上,更改周围的查询字符串参数不会导致错误,而是DBAgent.authenticate()将按顺序接受参数,而不管查询字符串中的参数名称如何 我错过了什么 IDBAgent.cs:C# 如何在WCF服务中使用查询字符串?,c#,wcf,query-string,C#,Wcf,Query String,我正在尝试创建web服务,到目前为止它们工作得很好,但我有一个问题:如果我的查询字符串的顺序与我指定的顺序不一致,那么代码会给我错误的结果 在开始一个大型项目之前,我希望能够传入一个查询字符串,这样顺序就无关紧要了-传入“?user=foo&pass=bar”应该相当于“?pass=bar&user=foo”,但我不确定如何让它按预期工作 事实上,更改周围的查询字符串参数不会导致错误,而是DBAgent.authenticate()将按顺序接受参数,而不管查询字符串中的参数名称如何 我错过了什么
public interface IDBAgent
{
[OperationContract]
[WebInvoke(UriTemplate = "/authenticate/?username={username}&password={password}", Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
string authenticate(string username, string password);
}
DBAgent.svc.cs
public class DBAgent : IDBAgent
{
public string authenticate(string username, string password)
{
return runSQL("EXEC sp_Authenticate '" + username + "', '" + password + '\'');
}
}
index.html:
var output = "";
function callService(url, params)
{
try {
return $.ajax({
type: "GET",
async: false,
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
processdata: true,
success: function (msg) {
output = msg;
},
error: function(){console.log("Oops.")}
});
}
catch (e) {
console.log("Something went wrong! (╯°□°)╯︵ ┻━┻");
}
}
function authenticate(user, pass)
{
callService("http://localhost/DBAgent.svc/authenticate/?username=foo&password=bar", []).done(); // Returns true
console.log(output);
callService("http://localhost/DBAgent.svc/authenticate/?password=bar&username=foo", []).done(); // Returns false
console.log(output);
callService("http://localhost/DBAgent.svc/authenticate/?password=foo&username=bar", []).done(); // Returns true
console.log(output);
}
对于这种情况,您可能应该考虑使用“POST”而不是“GET”。另外,请注意,在使用UriTemplate时,querystring参数的顺序并不重要。
“user=foo&pass=bar”在结构上等同于“pass=bar&user=foo”“,yikes您正在请求sql注入。GET对于身份验证也不是一个好主意。我一直在创建一个“弗兰肯斯坦怪兽”式的代码,将代码片段拼凑在一起,学习它们是如何工作的。我还没有转换成POST或防止SQL注入;我想这是在我得到了一些基本的东西后发生的=/当你说“顺序不重要”时,我有点困惑-我的js代码显示了我正在使用的确切代码,但身份验证是按照参数出现的顺序抓取参数(不管它们前面的标签如何)。“?user=foo&pass=bar”和“?pass=foo&user=bar”的计算值相同。我不确定到底发生了什么,但在使用它一段时间后,这是有效的。也许是PEBKAC,但谢谢!