比如,只允许指定的程序访问返回数据?
------解决方案--------------------
如一楼所说,票据认证即可。
思路就是,首先客户端程序在第一次调用你的web服务方法时,你要验证它的信息是否正确,比如,它提供了用户名和密码,通常你web服务方要调用数据库中的注册信息验证用户名和密码是否正确。正确的话,就返回一个凭证,简单的做法就是用Guid生成一个随机码。web服务端缓存该随机码,而客户端在得到该随机码后,在以后的每次调用中都要提供该随机码。这样就避免了每次都要提供用户名和密码,节省了时间也增强了安全性。给你一段例子:
- C# code
#region 客户端身份认证
[WebMethod(Description = "票据产生方法,客户端在调用其它方法之前必须先调用该方法认证其身份,验证成功的结果就是返回一个票据")]
public string GetTicket(string identity, string password)
{
//Authenticate the client
if (!Authenticate(identity, password))
{
throw new Exception("Invalid identity/Password");
}
Guid gTicket = Guid.NewGuid();
this.Context.Cache.Insert(gTicket.ToString(), true);
return gTicket.ToString();
}
/// <summary>
/// 验证票据
/// </summary>
/// <param name="ticket"></param>
/// <returns></returns>
private bool Authenticate(string ticket)
{
bool bRet = false;
try
{
if ((bool)Context.Cache.Get(ticket))
{
bRet = true;
}
}
catch (Exception ex)
{
throw ex;
}
return bRet;
}
/// <summary>
/// 获取票据之前到数据库验证客户身份
/// </summary>
/// <param name="identity"></param>
/// <param name="password"></param>
/// <returns></returns>
private bool Authenticate(string identity, string password)
{
bool retAuth = false;
string SQLString = "SELECT * FROM DataProviders WHERE 标识码 = '"+identity+"' AND 口令 = '"+password+"'";
DataSet ds = DbHelperSQL.Search(SQLString);
if (ds.Tables[0].Rows.Count == 1)
retAuth = true;
return retAuth;
}
#endregion
[WebMethod]
public DataSet SearchWithSQL(string SQLString,string ticket)
{
//在执行方法体响应之前验证票据有效性
if (!Authenticate(ticket))
{
throw new Exception("Invalid Ticket");
}
return DbHelperSQL.Search(SQLString);
}