服务端 /// <summary> /// ÓÃÓÚDataServiceµÄSOAPÊý¾ÝÍ·£» /// </summary> public class DataServiceSOAPHeader: SoapHeader { public string Username; public string Password; }
/// <summary> /// Summary description for DataService. /// </summary> [WebService(Namespace="http://www.sophia.com.cn/GeneralService/DataService")] public class DataService : System.Web.Services.WebService { ///must define it to use!!!!!!!!! public DataServiceSOAPHeader Authenticate;
... [WebMethod(Description="open recordset by the special SQL")] [SoapHeader("Authenticate", Required=true)] public string OpenSQL(string szSQL) { string result = "";
if ((Authenticate.Username == "...") && (Authenticate.Password = "...")) { ...
1.在web.config中将认证设为Windows(这里假定使用Windows认证);
2.在IIS中,将WebService的匿名访问disable;
3.不要改动WebService目录的文件权限(尽管MSDN里要求这样做),不然Web Service会不能访问;
4.在IIS中,设置登陆方式(Windows自动登陆或手工登陆)。
好了,你可以到另一台机器上测试以下。打开IE,敲入http://yourmachine/yourservice,会弹出登陆窗口,如果这台机器不是正好用户名和密码都和server机相同的话。
5.事情还远没有完。由于连接Web Service的一般是软件,而程序在调用Web Service之前,必须传递证书。如果你用.NET C#开发客户端,那么你会用.NET建立一个Web reference,例如你建了一个MyService1指向你的Web Service。那么,在调用MyService1.******之前,你必须先用NetWorkCred*****(名字忘了,查MSDN吧)建立一个证书,或者用DefaultCred.....得到当前Windows的证书,然后赋给 MyService1.Cred.... 。这就成了。其实,这些只是认证的表象而已。真正的认证过程微软把它给黑箱了,不让我们看到,所以用别的平台就根本无法认证,也就无法连需要认证的.NET Web Service.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwssecur/html/securitywhitepaper.asp
服务端
/// <summary>
/// ÓÃÓÚDataServiceµÄSOAPÊý¾ÝÍ·£»
/// </summary>
public class DataServiceSOAPHeader: SoapHeader {
public string Username;
public string Password;
}
/// <summary>
/// Summary description for DataService.
/// </summary>
[WebService(Namespace="http://www.sophia.com.cn/GeneralService/DataService")]
public class DataService : System.Web.Services.WebService
{
///must define it to use!!!!!!!!!
public DataServiceSOAPHeader Authenticate;
...
[WebMethod(Description="open recordset by the special SQL")]
[SoapHeader("Authenticate", Required=true)]
public string OpenSQL(string szSQL)
{
string result = "";
if ((Authenticate.Username == "...") && (Authenticate.Password = "...")) {
...
使用WSDL生成指定WebServices的c#代码,该代码将形成该服务的一个类,内容非常直观,一眼就明白,就不列了。
如果你的Web Method属于多种类型(如管理员的、用户的...)不宜放在一个class中,你可以写一个base class,把UserLogOn(string userName, string passWord)做到它的constractor里面,其他含有Web Method的class使用继承的方式同样也可以实现你的需求