0

現在、独自の 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 の場合は拒否します。

「メッセージ」を確認しましたが、次のようになります。

  • 復号化する必要があります
  • バインディングに大きく依存しているようです

パラメータの型を取得する簡単な方法はありますか?

4

1 に答える 1

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を使用できます。

于 2010-02-11T18:58:33.860 に答える