我正在尝试为ASMXweb服务实现基本授权。我在VS2015中创建了客户端作为服务引用。我在Asmx web service basic authentication中使用代码作为示例。
我在ClientCredentials中输入登录信息,如下所示
Dim svc As New WebServiceSoapClient()
svc.ClientCredentials.UserName.UserName = "userId"
svc.ClientCredentials.UserName.Password = "i2awTieS0mdO"
我的问题是,在Web服务的Authorization HttpModule中,这些凭据未传递给模块。有替代方法吗?
答案 0 :(得分:0)
我在How to add HTTP Header to SOAP Client找到了部分答案。我必须在该页面上结合几个答案才能使其发挥作用。
Dim svc As New WebServiceSoapClient()
Dim responseService As SoapResponseObject
Using (new OperationContextScope(svc.InnerChannel))
Dim auth = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("userId:i2awTieS0mdO"))
Dim requestMessage = New HttpRequestMessageProperty()
requestMessage.Headers("Authorization") = auth
OperationContext.Current.OutgoingMessageProperties(HttpRequestMessageProperty.Name) = requestMessage
dim aMessageHeader = MessageHeader.CreateHeader("Authorization", "http://tempuri.org", auth)
OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader)
responseService = svc.ListDistricts(requestService)
End Using
需要注意的一件事是soap客户端调用必须在Using语句中。在上面的代码中,这是最后一行的下一个。