使用网络凭据VB.Net获取Web服务进行身份验证

时间:2014-08-22 16:27:52

标签: vb.net web service

以下VB.net代码正在对Web服务进行身份验证 - 它运行良好,但我必须使用默认网络凭据(代码运行的登录用户)

我想将网络信用传递给网络服务,以下内容应该有效,但不能。这是使用默认凭据的工作代码。

    Dim URL As String = "http://server/MBSDKService/MsgSDK.asmx?WSDL"
    Dim myService As New LANDeskMBDSK.MBSDK
    myService.Url = URL
    Dim CredCache As New System.Net.CredentialCache
    myService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials

这就是我想要的工作,但不是......

    Dim URL As String = "http://server/MBSDKService/MsgSDK.asmx?WSDL"
    Dim myService As New LANDeskMBDSK.MBSDK
    myService.Url = URL
    Dim Cred As New System.Net.NetworkCredential("domain\user", "password")
    Dim CredCache As New System.Net.CredentialCache
    CredCache.Add(New Uri(myService.Url), "Basic", Cred)
    myService.Credentials = CredCache

有谁知道我做错了什么?

其余的代码完美无缺,所以这是我必须添加到上面的内容,以使其像默认凭据一样工作。

谢谢!

感谢George,这是解决方案......

    Dim URL As String = "http://server/MBSDKService/MsgSDK.asmx?WSDL"
    Dim myService As New LANDeskMBDSK.MBSDK
    myService.Url = URL
    Dim Cred As New System.Net.NetworkCredential("user", "password")
    Dim CredCache As New System.Net.CredentialCache
    CredCache.Add(New Uri(myService.Url), "Basic", Cred)
    myService.Credentials = Cred

1 个答案:

答案 0 :(得分:0)

尝试以下任何一项:

Dim Cred As New System.Net.NetworkCredential("user", "password", "domain")

Dim Cred As New System.Net.NetworkCredential("user", "password")

什么是实际的错误消息?

编辑:

这似乎有效:

myService.Credentials = Cred