通过邮件头的WCF安全性

时间:2010-04-07 14:09:58

标签: wcf security header

我正在尝试实施“某种”服务器 - 客户端&某些WCF服务的零配置安全性。

我在www上找到的最好的(也是最简单的)解决方案是在http://www.dotnetjack.com/post/Automate-passing-valuable-information-in-WCF-headers.aspx(客户端)和http://www.dotnetjack.com/post/Processing-custom-WCF-header-values-at-server-side.aspx(对应的服务器端)中描述的解决方案。

以下是我对RequestAuth的实现(在上面的第一个链接中描述):

using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

namespace AuthLibrary
{
    /// <summary>
    /// Ref: http://www.dotnetjack.com/post/Automate-passing-valuable-information-in-WCF-headers.aspx
    /// </summary>
    public class RequestAuth : BehaviorExtensionElement, IClientMessageInspector, IEndpointBehavior
    {
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string headerName = "AuthKey";

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string headerNamespace = "http://some.url";

        public override Type BehaviorType
        {
            get { return typeof(RequestAuth); }
        }

        protected override object CreateBehavior()
        {
            return new RequestAuth();
        }

        #region IClientMessageInspector Members

        // Keeping in mind that I am SENDING something to the server,
        // I only need to implement the BeforeSendRequest method

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            throw new NotImplementedException();
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            MessageHeader<string> header = new MessageHeader<string>();
            header.Actor = "Anyone";
            header.Content = "TopSecretKey";

            //Creating an untyped header to add to the WCF context 
            MessageHeader unTypedHeader = header.GetUntypedHeader(headerName, headerNamespace);

            //Add the header to the current request 
            request.Headers.Add(unTypedHeader);

            return null;
        }

        #endregion

        #region IEndpointBehavior Members

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            throw new NotImplementedException();
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(this);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            throw new NotImplementedException();
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

首先,我将此代码放在我的客户端WinForms应用程序中,但后来我在签名时遇到了问题,因为我必须在所有第三方引用中签名http://msdn.microsoft.com/en-us/library/h4fa028b(v=VS.80).aspx在“不应该命名的内容”部分“陈述:

  

通常,您应该避免强命名应用程序EXE程序集。强命名的应用程序或组件不能引用弱命名的组件,因此强命名EXE会阻止EXE引用随应用程序部署的弱命名的DLL。

     

因此,Visual Studio项目系统没有强名称应用程序EXE。相反,它强烈命名应用程序清单,该清单内部指向弱命名的应用程序EXE。

我希望VS避免这个问题,但我没有运气,它抱怨所有未签名的引用,所以我在我的解决方案中创建了一个单独的“WCF服务库”项目,其中只包含上面的代码并签署了该代码。< / p>

此时整个解决方案编译得还不错。

这是我的问题:

当我启动“WCF服务配置编辑器”时,我能够添加新的行为元素扩展名(比如“AuthExtension”),但是当我尝试将该扩展名添加到我的终点行为时,它给了我:

  

调用目标抛出了异常。

所以我被困在这里。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

你有一些:

        throw new NotImplementedException(); 
你的代码中的

。这些可能是被抛出的例外。尝试删除它们,看看是否收到同样的错误。

答案 1 :(得分:1)

Shiraz Bhaiji是对的。框架确实调用那些你没有实现异常的方法。删除它。

相关问题