Windows Phone Silverlight 8.0 - 在BasicHttpBinding上将ClientCredentialType指定为Basic

时间:2014-09-10 09:17:41

标签: c# windows-phone-8 basichttpbinding

我有一个具有授权的SOAP WebService,并要求我将ClientCredentialType指定为HttpClientCredentialType.Basic。

我正常的.NET项目我可以指定它(在代码或XML中)连接到我的WebService:

BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
mySoapClient = new MySoapClient(binding, address);

问题是在可移植类库(PCL)或Windows Phone 8.0 Silverlight C#项目中,属性Transport不存在。

有解决方法吗?

由于

1 个答案:

答案 0 :(得分:0)

找出答案。 看起来所有请求都必须在OperationContextScope中执行。

        private void InvokeWebMethod(Action webMethod, string username = null, string password = null)
        {
           using (OperationContextScope contextScope = new OperationContextScope(presenceServer.InnerChannel))
            {
                var bc = Encoding.UTF8.GetBytes(string.Format(@"{0}:{1}", username ?? this.Username, password ?? this.Password));
                HttpRequestMessageProperty httpProps = new HttpRequestMessageProperty();
                httpProps.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(bc);

                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpProps;
                webMethod();
            }
        }

感谢。