简体   繁体   English

通过邮件头的WCF安全性

[英]WCF security via message headers

I'm trying to implement "some sort of" server-client & zero-config security for some WCF service. 我正在尝试为某些WCF服务实现“某种”服务器 - 客户端和零配置安全性。

The best (as well as easiest to me) solution that I found on www is the one described at http://www.dotnetjack.com/post/Automate-passing-valuable-information-in-WCF-headers.aspx (client-side) and http://www.dotnetjack.com/post/Processing-custom-WCF-header-values-at-server-side.aspx (corrisponding server-side). 我在www上找到的最好的(也是最简单的)解决方案是在http://www.dotnetjack.com/post/Automate-passing-valuable-information-in-WCF-headers.aspx (客户端)中描述的解决方案。 -side)和http://www.dotnetjack.com/post/Processing-custom-WCF-header-values-at-server-side.aspx (相应的服务器端)。

Below is my implementation for RequestAuth (descibed in the first link above): 下面是我对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
    }
}

So first I put this code in my client WinForms application, but then I had problems signing it, because I had to sign also all third-party references eventhough http://msdn.microsoft.com/en-us/library/h4fa028b(v=VS.80).aspx at section "What Should Not Be Strong-Named" states: 所以首先我将此代码放在我的客户端WinForms应用程序中,但后来我在签名时遇到了问题,因为我必须签署所有第三方引用,尽管http://msdn.microsoft.com/en-us/library/h4fa028b( v = VS.80).aspx在“什么不应该强名”部分说明:

In general, you should avoid strong-naming application EXE assemblies. 通常,您应该避免强命名应用程序EXE程序集。 A strongly named application or component cannot reference a weak-named component, so strong-naming an EXE prevents the EXE from referencing weak-named DLLs that are deployed with the application. 强命名的应用程序或组件不能引用弱命名的组件,因此强命名EXE会阻止EXE引用随应用程序部署的弱命名的DLL。

For this reason, the Visual Studio project system does not strong-name application EXEs. 因此,Visual Studio项目系统没有强名称应用程序EXE。 Instead, it strong-names the Application manifest, which internally points to the weak-named application EXE. 相反,它强烈命名应用程序清单,该清单内部指向弱命名的应用程序EXE。

I expected VS to avoid this problem, but I had no luck there, it complained about all the unsigned references, so I created a separate "WCF Service Library" project inside my solution containing only code above and signed that one. 我希望VS能够避免这个问题,但我没有运气,它抱怨所有未签名的引用,所以我在我的解决方案中创建了一个单独的“WCF服务库”项目,其中只包含上面的代码并签署了该代码。

At this point entire solution compiled just okay. 在这一点上,整个解决方案编译得还可以。

And here's my problem: 这是我的问题:

When I fired up "WCF Service Configuration Editor" I was able to add new behavior element extension (say "AuthExtension"), but then when I tried to add that extension to my end point behavior it gives me: 当我启动“WCF服务配置编辑器”时,我能够添加新的行为元素扩展(比如说“AuthExtension”),但是当我尝试将该扩展添加到我的终点行为时,它给了我:

Exception has been thrown by the target of an invocation. 调用的目标抛出了异常。

So I'm stuck here. 所以我被困在这里。

Any ideas? 有任何想法吗?

You have some: 你有一些:

        throw new NotImplementedException(); 

in your code. 在你的代码中。 These could be the exceptions that are being thrown. 这些可能是被抛出的例外。 Try removing these and see if you get the same error. 尝试删除这些,看看是否得到相同的错误。

Shiraz Bhaiji is right. Shiraz Bhaiji是对的。 The framework does call those methods that you are throwing not implemented exceptions. 框架确实调用那些你没有实现异常的方法。 Remove that. 删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM