首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于服务调用参数的AuthorizationManager

基于服务调用参数的AuthorizationManager
EN

Stack Overflow用户
提问于 2010-02-11 17:56:25
回答 1查看 945关注 0票数 0

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

代码语言:javascript
复制
 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);
    }
}

我想根据服务合同参数执行授权检查,例如,如果合同看起来是:

代码语言:javascript
复制
[ServiceContract]
public interface IServerContract
{
    [OperationContract]
    [ServiceKnownType(typeof(ChildTypeOne))]
    [ServiceKnownType(typeof(ChildTypeTwo))]
    string SecuredMessage(ParentType incoming);
}

我的目标是根据类型授权,例如,如果传入日期是ChildTypeOne,则授权,如果是ChildTypeTwo,则拒绝。

我检查了“留言”,看起来是:

必须是decrypted

  • Seems才能高度依赖于绑定

是否有简单获取参数类型的简单方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-02-11 18:58:33

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

下面是我使用的AuthorizationManager:

代码语言:javascript
复制
 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来获得基于类型的角色。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2246453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档