webmethods体外的Web服务认证

时间:2012-05-17 10:22:16

标签: .net web-services authentication

我们公司有一个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
}

但我必须在每种方法中都这样做......我必须在每个新方法和每个新的Web服务中记住它。所以问题是:
有没有办法设计我的课程,以便在每次明确调用check in方法的主体时调用任何方法时都会执行此检查?
嗯,我不这么认为,但总是值得问。

1 个答案:

答案 0 :(得分:0)

您可以将AOP与PostSharp,Unity或Castle等工具一起使用来拦截或创建动态代理。这可能会迫使您重做一些服务来实现接口。我只有Unity的经验,但是从界面实现然后用Aspects连接可能会有效。

也许这也有帮助:Best free way to use AOP style MVC-like authorization in business logic