代码之家  ›  专栏  ›  技术社区  ›  NAADEV

基于服务调用参数的授权管理器

  •  0
  • NAADEV  · 技术社区  · 15 年前

    我目前正在开发自己的授权管理器,它看起来是这样的:

     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,则授权,如果传入日期是childtype2,则拒绝。

    我检查了“信息”,它看起来像:

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

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

    1 回复  |  直到 15 年前
        1
  •  0
  •   NAADEV    15 年前

    好吧,我已经知道怎么做了。无论如何,如果你知道更好的方法,请告诉我:

    这是我使用的授权管理器:

     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");
            }
        }
    }
    

    先前的授权经理将拒绝“ChildType2”。可以使用RoleProvider根据类型获取角色。

    推荐文章