公司叫我和另一个网站写一个用户接口,如下:
代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace UserLoginTest
{
/// <summary>
/// CheckUser 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class CheckUser : System.Web.Services.WebService
{
/// <summary>
/// 不要验证的方法
/// </summary>
/// <returns></returns>
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
public MySoapHeader soapHead = new MySoapHeader();
/// <summary>
///需要验证的方法
/// </summary>
/// <param name="UserID"></param>
/// <param name="UserName"></param>
/// <param name="msg"></param>
/// <returns></returns>
[SoapHeader("soapHead")]
[WebMethod]
public bool GetUserInfo(int UserID, string UserName, out string msg)
{
string deUserName = EncryptHelper.Decrypt(UserName, "jjjyzy");
msg = string.Empty;
if (!soapHead.IsValid(out msg))
{ return false; }
else
{
return checkCount(UserID, deUserName);
}
}
/// <summary>
/// 查询ID和帐号
/// </summary>
private bool checkCount(int userID, string Username)
{
string sqlStr = "select table where id=userid and Username= username";
return true;
}
}
/// <summary>
/// soaphead验证的方法帐号密码ly(重写一个类)
/// </summary>
public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{
public string WsUserName
{ get; set; }
public string WsPwd
{ get; set; }
public MySoapHeader()
{
}
public bool IsValid(out string msg)
{
return IsValid(WsUserName, WsPwd, out msg);
}
private bool IsValid(string wsusername, string wspwd, out string msg)
{//, out string msg
msg = string.Empty;
if (wsusername == "ly" & wspwd == "ly")
{ msg = "您有权调用"; return true; }
else { msg = "您无权调用"; return false; }
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace UserLoginTest
{
/// <summary>
/// CheckUser 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class CheckUser : System.Web.Services.WebService
{
/// <summary>
/// 不要验证的方法
/// </summary>
/// <returns></returns>
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
public MySoapHeader soapHead = new MySoapHeader();
/// <summary>
///需要验证的方法
/// </summary>
/// <param name="UserID"></param>
/// <param name="UserName"></param>
/// <param name="msg"></param>
/// <returns></returns>
[SoapHeader("soapHead")]
[WebMethod]
public bool GetUserInfo(int UserID, string UserName, out string msg)
{
string deUserName = EncryptHelper.Decrypt(UserName, "jjjyzy");
msg = string.Empty;
if (!soapHead.IsValid(out msg))
{ return false; }
else
{
return checkCount(UserID, deUserName);
}
}
/// <summary>
/// 查询ID和帐号
/// </summary>
private bool checkCount(int userID, string Username)
{
string sqlStr = "select table where id=userid and Username= username";
return true;
}
}
/// <summary>
/// soaphead验证的方法帐号密码ly(重写一个类)
/// </summary>
public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{
public string WsUserName
{ get; set; }
public string WsPwd
{ get; set; }
public MySoapHeader()
{
}
public bool IsValid(out string msg)
{
return IsValid(WsUserName, WsPwd, out msg);
}
private bool IsValid(string wsusername, string wspwd, out string msg)
{//, out string msg
msg = string.Empty;
if (wsusername == "ly" & wspwd == "ly")
{ msg = "您有权调用"; return true; }
else { msg = "您无权调用"; return false; }
}
}
}