0

I am using a .NET Web Reference for consuming a web service that is using basic authentication. In the past I could set the credentials by accessing the same-named property. For example:

MyWebReference.Service1 client = new MyWebReference.Service1();
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.MyOperation();

This worked fine in an older project. Now I am trying to set the Credentials block in a new VS project and I can't find the property anymore. I checked my old project and there were several properties like AllowAutoRedirect, ClientCertificates, ConnectionGroupName, Container, CookieContainer, ...

In my new VS project I just got Url and UseDefaultCredentials. I also included the web service from the older (working) VS project into my new project, but there are no properties either. So it must be something strange with the VS project or there are references missing.

I checked references, recreated the web reference several times but didn't get things working.

I am using Visual Studio 2010 Professional.

Any idea?

Thanks for your help

nvm-uli
  • 626
  • 1
  • 7
  • 14

2 Answers2

0

Make sure your Service class inherits from: System.Net.HttpWebRequest This has properties that you are looking for.

using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Web.Services;
using System.Web.UI;

namespace WebApplication4
{
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MyService math = new MyService()
            {
                Credentials = new NetworkCredential("Joe", "mydomain", "password"),
                AllowAutoRedirect = false,
                ClientCertificates = new X509CertificateCollection(),
                ConnectionGroupName = ""
            };
        }
    }

    [WebService(Namespace = "http://tempuri.org/")]
    public class MyService : HttpWebRequest
    {
        [WebMethod]
        public int SumOfNums(int First, int Second)
        {
            return First + Second;
        }
    }
}

Reference: MSDN

Pang
  • 9,564
  • 146
  • 81
  • 122
InCommand
  • 41
  • 1
  • 1
  • 9
  • The service I'm trying to consume was not developed from me. But it must be something in the client, because the same service has the `Credentials` property in another project. – nvm-uli Nov 03 '14 at 08:04
  • [MSDN](http://msdn.microsoft.com/de-de/library/system.web.services.protocols.soaphttpclientprotocol(v=vs.110).aspx) My client inherits from `SoapHttpClientProtocol` which inherits the `Credentials` property from `WebClientProtocol`. So I can't understand why this property is not accessable. – nvm-uli Nov 03 '14 at 08:18
0

I resolved the problem by myself. Unfortunately I had added a WCF Service named System.svc to the project map which resulted in a conflict between the System namespace and the class named System

I renamed the System.svc class and the Credentials-property became accessible.

nvm-uli
  • 626
  • 1
  • 7
  • 14