2

I have recently been writing a client for a WSS service. It is using the usernametoken profile according to it's documentation. And I have written something like this as a code to communicate it. But due to not allowing of anonymous authentication I have always faced the unauthorized error by the service.
How can I work this out?

Here is the sample code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Execute();
        }
        public  void Execute()
        {
            HttpWebRequest request = CreateWebRequest();
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""UTF-8""?>
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
<soapenv:Header>
<wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
<wsse:UsernameToken>
<wsse:Username>something</wsse:Username>
<wsse:Password>something</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
...
</soapenv:Body>
</soapenv:Envelope>
");
            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    string soapResult = rd.ReadToEnd();
                    Console.WriteLine(soapResult);
                }
            }
        }
        public HttpWebRequest CreateWebRequest()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://127.0.0.1:5434");
            webRequest.Headers.Add(@"SOAP:Action");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }
    }
}

Any help is appreciated.

Miad Abrin
  • 952
  • 7
  • 14

1 Answers1

0

There are two solutions to implement UsernameToken Profile support for WCF:

So, why not use WCF instead of making raw requests?

  • The problem is that I can not add reference to the service because of the lack of anonymous authentication.So I will have no service model to use with WCF – Miad Abrin Jan 27 '13 at 07:27
  • Can you download **wsdl** file somehow (I mean not just from code, but manually)? If yes, just generate the proxy code using **svcutil** (note: use **XmlSerializer** for svcutil). – Sergey Vyacheslavovich Brunov Jan 27 '13 at 08:44
  • I used svcutil to create the proxy. just as I expected it,the service returned the unauthorized error again . As I said. No anonymous access is allowed. but thanks for your help anyway – Miad Abrin Jan 27 '13 at 10:00