为了使.asmx句柄有可能反串行化SOAP头,首先你需要定义一个.NET类,它代表了暗含的XML Schema类。在此例中相应的类如下:
[XMLType(Namespace="http://example.org/security")]
[XMLRoot(Namespace="http://example.org/security")]
public class UsernameToken : SoapHeader {
public string username;
public string password;
}
然后你需要在WebMethod类中定义一个成员变量来控制一个头类的实例,同样要为WebMethods标记[SoapHeader]属性。见如下:
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace="urn:geometry")]
public class Geometry {
public UsernameToken Token;
[WebMethod]
[SoapHeader("Token")]
public double Distance(Point orig, Point dest) {
if (!Token.username.Equals(Reverse(Token.password)))
throw new Exception("access denied");
return Math.Sqrt(Math.Pow(orig.x-dest.x, 2) +
Math.Pow(orig.y-dest.y, 2));
}
}
[XMLType(Namespace="http://example.org/security")]
[XMLRoot(Namespace="http://example.org/security")]
public class UsernameToken : SoapHeader {
public string username;
public string password;
}
然后你需要在WebMethod类中定义一个成员变量来控制一个头类的实例,同样要为WebMethods标记[SoapHeader]属性。见如下:
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace="urn:geometry")]
public class Geometry {
public UsernameToken Token;
[WebMethod]
[SoapHeader("Token")]
public double Distance(Point orig, Point dest) {
if (!Token.username.Equals(Reverse(Token.password)))
throw new Exception("access denied");
return Math.Sqrt(Math.Pow(orig.x-dest.x, 2) +
Math.Pow(orig.y-dest.y, 2));
}
}