AuthorizationManager基于服务调用参数

时间:2010-02-11 17:56:26

标签: wcf-security authorizationmanager

我目前正在开发自己的AuthorizationManager,它看起来像这样:

 public class MyAuthorizationManager : ServiceAuthorizationManager
{
    static bool initialize = false;
    public override bool CheckAccess(OperationContext operationContext)
    {
        ServiceSecurityContext context = ServiceSecurityContext.Current;
        string[] roles = Roles.GetRolesForUser(operationContext.ServiceSecurityContext.PrimaryIdentity.Name);
        return roles.Count() > 0;
    }

    public override bool CheckAccess(OperationContext operationContext, ref System.ServiceModel.Channels.Message message)
    {
        MessageBuffer buffer = operationContext.RequestContext.RequestMessage.CreateBufferedCopy(int.MaxValue);
        message = buffer.CreateMessage();
        Console.WriteLine(message);
        return base.CheckAccess(operationContext, ref message);
    }
}

我想基于服务合同参数执行授权检查,例如,如果合同如下所示:

[ServiceContract]
public interface IServerContract
{
    [OperationContract]
    [ServiceKnownType(typeof(ChildTypeOne))]
    [ServiceKnownType(typeof(ChildTypeTwo))]
    string SecuredMessage(ParentType incoming);
}

我的目标是根据类型进行授权,例如,授权传入日期是否为ChildTypeOne,如果是ChildTypeTwo则为deniying。

我已查看“消息”,它看起来像:

  • 必须解密
  • 似乎高度依赖于绑定

有简单的方法来获取参数类型吗?

1 个答案:

答案 0 :(得分:0)

好的,我已经想出了如何执行该操作。无论如何,如果您知道更好的方法,请告诉我:

这是我正在使用的AuthorizationManager:

 public class MyAuthorizationManager : ServiceAuthorizationManager
{
    static bool initialize = false;

    public override bool CheckAccess(OperationContext operationContext, ref System.ServiceModel.Channels.Message message)
    {
            bool returnedValue = base.CheckAccess(operationContext, ref message);
            // messags in WCF are always read-once
            // we create one copy to work with, and one copy to return back to the plumbing
            MessageBuffer buffer = operationContext.RequestContext.RequestMessage.CreateBufferedCopy(int.MaxValue);
            message = buffer.CreateMessage();

            // get the username vale using XPath
            XPathNavigator nav = buffer.CreateNavigator();
            StandardNamespaceManager nsm = new StandardNamespaceManager(nav.NameTable);
            nav = nav.SelectSingleNode("//@i:type",nsm);
            returnedValue &= (nav.ToString() == "a:"+typeof(ChildTypeOne).Name);
            return returnedValue;
    }


    public class StandardNamespaceManager : XmlNamespaceManager
    {
        public StandardNamespaceManager(XmlNameTable nameTable)
            : base(nameTable)
        {
            this.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
            this.AddNamespace("s11", "http://schemas.xmlsoap.org/soap/envelope/");
            this.AddNamespace("s12", "http://www.w3.org/2003/05/soap-envelope");
            this.AddNamespace("wsaAugust2004", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
            this.AddNamespace("wsa10", "http://www.w3.org/2005/08/addressing");
            this.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");
        }
    }
}

以前的AuthorizationManager将拒绝“ChildTypeTwo”。您可以使用RoleProvider来获取基于类型的角色。