setting basic authentication c# to call a soap web service

730 Views Asked by At

As suggested by Julian i accept this is not much different but still face difficulty may be because of the nature of the web service the client has exposed. anyway this is my latest code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            sherpa_service.SherpaInstanceType create_instance = new sherpa_service.SherpaInstanceType();
            sherpa_service.StringType category = new sherpa_service.StringType();
            sherpa_service.StringType assignment_group = new sherpa_service.StringType();
            sherpa_service.StringType assignee = new sherpa_service.StringType();
            sherpa_service.StringType area = new sherpa_service.StringType();
            sherpa_service.StringType sub_area = new sherpa_service.StringType();
            sherpa_service.StringType impact = new sherpa_service.StringType();
            sherpa_service.StringType urgency = new sherpa_service.StringType();
            sherpa_service.StringType contact_number = new sherpa_service.StringType();
            sherpa_service.StringType title = new sherpa_service.StringType();
            sherpa_service.StringType email_id = new sherpa_service.StringType();
            sherpa_service.StringType[] desc = new sherpa_service.StringType[3];
            desc[0] = new sherpa_service.StringType();
            desc[0].Value = "sample description from .net";
            sherpa_service.SherpaInstanceTypeDescription description = new sherpa_service.SherpaInstanceTypeDescription();
            description.Description = desc;




            category.Value = "Finance / Treasury / Tax Apps";
            assignment_group.Value = "Procure to Pay";
            area.Value = "Finance Apps";
            sub_area.Value = "concur";
            assignee.Value = "apadiyar";
            email_id.Value = "[email protected]";
            contact_number.Value = "9884525412";

            create_instance.ContactNumber = contact_number;
            create_instance.EXternalEmail = email_id;
            create_instance.Category = category;
            create_instance.AssignmentGroup = assignment_group;
            create_instance.Assignee = assignee;
            create_instance.Area = area;
            create_instance.Subarea = sub_area;
            create_instance.Description = description;

            sherpa_service.SherpaKeysType create_key = new sherpa_service.SherpaKeysType();
            sherpa_service.SherpaModelType create_model = new sherpa_service.SherpaModelType();
            sherpa_service.CreateSherpaRequest1 create_incident = new sherpa_service.CreateSherpaRequest1();

            create_model.keys = create_key;
            create_model.instance = create_instance;
            create_incident.CreateSherpaRequest = new sherpa_service.CreateSherpaRequest();
            create_incident.CreateSherpaRequest.model = create_model;
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.SendTimeout = TimeSpan.FromSeconds(25);

            //binding.Security.Mode = BasicHttpSecurityMode.Transport;
            binding.Security.Transport.ClientCredentialType =
                                          HttpClientCredentialType.Basic;
            EndpointAddress address = new EndpointAddress("http://xxxxxxxxxxxxxxxxx/SM/7/ws");

            ChannelFactory<sherpa_service.Sherpa> factory =
             new ChannelFactory<sherpa_service.Sherpa>(binding, address);
            factory.Credentials.UserName.UserName = "xxx";
            factory.Credentials.UserName.Password = "xxx";
            sherpa_service.Sherpa proxy = factory.CreateChannel();
            sherpa_service.CreateSherpaResponse1 respo = proxy.CreateSherpa(create_incident);
            Console.WriteLine(respo);
        }
    }
}

Below is the exception almost back to square 1! am still referring the link you had given , but hardly i could figure out the mistake .

System.ServiceModel.Security.MessageSecurityException: 'The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="CASM"'.'

WebException: The remote server returned an error: (401) Unauthorized.

please see this image to seethe multiple classes created.

also i want to ask then what is the purpose of the "sherpa client" class created by the .net IDE itself when i first added the web reference.?

0

There are 0 best solutions below