我们公司有一个 Web 服务,我们想发布它以供其他开发团队使用。尽管如此,我们仍希望控制谁使用它以及是否允许他调用此特定方法。
因此,我们提出了一个解决方案,为每个想要使用我们的 Web 服务的团队分配一个键,该键告诉调用者可以使用该键调用哪些方法。该键将作为参数传递给每个方法。将此密钥作为参数,我可以在每个方法的开头执行安全检查,如下所示:
private bool CanCallMethod(string methodIdentifier, string authenticationKey)
{
//check in db
}
public object GetLocation(string authenticationKey, int param1, string param2)
{
if (!CanCallMethod(someMethodIdentifier, authenticationKey))
throw new UnauthorizedAccessException();
//method body
}
public object SetLocation(string authenticationKey, DateTime param1)
{
if (!CanCallMethod(someOtherMethodIdentifier, authenticationKey))
throw new UnauthorizedAccessException();
//method body
}
但我必须在每一种方法中都这样做……而且我必须在每一种新方法和每一种新的网络服务中记住它。所以问题是:
有什么方法可以设计我的类,以便在每次显式调用检查方法主体时在没有我的情况下调用任何方法时执行此检查?
好吧,我不这么认为,但总是值得一问。