如何传递凭据以使用asmx服务?

时间:2015-01-09 04:48:16

标签: asp.net web-services soap asp.net-web-api asmx

我正在尝试在需要身份验证的控制台应用程序中使用SAP Web服务。我将服务引用添加到控制台应用程序。

        ServiceReference1.SAPServiceSoapClient myService = new ServiceReference1.SAPServiceSoapClient();

        System.Net.CredentialCache myCredentials = new System.Net.CredentialCache();

        NetworkCredential netCred = new NetworkCredential("UserName", "Password");

        Uri uriPrefix = new Uri("url");

        myCredentials.Add(uriPrefix, "Basic", netCred);

       //The following line is giving error that ClientCredentials cannot be assigned as it is read only.            

        myService.ClientCredentials = myCredentials;

1 个答案:

答案 0 :(得分:0)

我们可以使用以下代码:

AuthenticationToken token=new AuthenticationToken();
token.UserName="username";
token.Password="password";