【发布时间】:2008-09-24 22:50:31
【问题描述】:
情况是这样的。我有一个 web 服务(C# 2.0),它(主要)由一个继承自 System.Web.Services.WebService 的类组成。它包含一些方法,它们都需要调用一个方法来检查它们是否被授权。
基本上是这样的(请原谅架构,这纯粹是一个例子):
public class ProductService : WebService
{
public AuthHeader AuthenticationHeader;
[WebMethod(Description="Returns true")]
[SoapHeader("AuthenticationHeader")]
public bool MethodWhichReturnsTrue()
{
if(Validate(AuthenticationHeader))
{
throw new SecurityException("Access Denied");
}
return true;
}
[WebMethod(Description="Returns false")]
[SoapHeader("AuthenticationHeader")]
public bool MethodWhichReturnsFalse()
{
if(Validate(AuthenticationHeader))
{
throw new SecurityException("Access Denied");
}
return false;
}
private bool Validate(AuthHeader authHeader)
{
return authHeader.Username == "gooduser" && authHeader.Password == "goodpassword";
}
}
如您所见,方法Validate 必须在每个方法中调用。我正在寻找一种能够调用该方法的方法,同时仍然能够以理智的方式访问肥皂标题。我查看了global.asax 中的事件,但我认为我无法访问该类中的标题...可以吗?
【问题讨论】:
标签: c# .net web-services