0

我有一个看起来像这样的定制肥皂头:

[DataContract()]
    public class IntegrationHeader
    {
        [DataMember]
        public string UserName;
        [DataMember]
        public string Password;
    }

然后我在 messageInspector 中有以下代码:

if (request.Headers.Action == null || request.Headers.Action.ToString().Length < 1)
                return null;

            foreach (var header in request.Headers)
            {
                if (header.Namespace == "ns" && header.Name == "SecurityToken")
                {
                    loginHandler = new LoginHandler();

                    integrationHeader = request.Headers.GetHeader<IntegrationHeader>(header.Name, header.Namespace);
                    if ((userContext = loginHandler.LoginUser(integrationHeader.UserName, PassWordManager.DESEncrypt(integrationHeader.Password), Business.Entity.LoginType.Regular)) == null)
                        throw new SecurityTokenException("Unknown username or invalid password");

                    Thread.CurrentPrincipal = userContext;

                    return null;
                }
            }

            throw new SecurityTokenException("Unknown username or invalid password");

使用soapui发送以下soap标头时,它可以正常工作:

   <soapenv:Header>
     <SecurityToken xmlns="ns" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
       <Password xmlns="MyApp.ServiceImplementation">X</Password>
       <UserName xmlns="MyApp.ServiceImplementation">X</UserName>
     </SecurityToken>
   </soapenv:Header>

然后我尝试从代理生成的 WCF 客户端中执行此操作:

使用 (client = new MyProxy()) {

                IntegrationHeader ih = new IntegrationHeader { UserName = "X", Password = "X" };

                MessageHeader untyped = MessageHeader.CreateHeader("SecurityToken", "ns", ih);
                using (new System.ServiceModel.OperationContextScope(client.InnerChannel))
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
                }

                client.GetData(request);
            }

问题是运行此代码时 messageInspector 中只有 2 个标头,并且它们都不是安全标头?

我究竟做错了什么?

4

1 回答 1

0

这是正确的解决方案:

            IntegrationHeader ih = new IntegrationHeader { UserName = "X", Password = "X" };

            MessageHeader untyped = MessageHeader.CreateHeader("SecurityToken", "ns", ih);
            using (new System.ServiceModel.OperationContextScope(client.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
                client.GetData(request);
            }

服务调用大部分在 using else 中完成,安全标头将从传出上下文中删除。

于 2012-09-17T08:57:36.263 回答