Wednesday, 20 November 2019

c# webclient NetworkCredentials in Nodejs

I have a c# .net Client with this Code:

using(WebClient client = new WebClient())
{
    string serialisedData = "";
    serialisedData = JsonConvert.SerializeObject(myData);
    client.Credentials = new NetworkCredential(config.UserData.Username, config.UserData.Password);
    byte[] responsebyte = client.UploadData(config.ServerAddress, System.Text.Encoding.UTF8.GetBytes(serialisedData));
}

That Client sends data to my nodejs Server. Nodejs Code:

var http = require('http');

var _server = http.createServer(_listener);
_server.listen(1234);

console.log( 'started' );

function _listener(req, res) {
  let data = []
  req.on('data', chunk => {
    data.push(chunk)
  })
  req.on('end', () => {
    data = Buffer.concat(data);
    var dataString = new Buffer.from(data).toString("utf-8");
    const data = JSON.parse(dataString);
    // data has all the data from the c# object "myData"
    res.write('response')
    res.end()
  })
}

But how can I access the credentials of this connection?


This is how I can Access the credentials in c#:

HttpListener listener = new HttpListener();
listener.Prefixes.Add($"https://+:{Config.Port}/");
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
listener.Start();
for (; ; )
{
    Console.WriteLine("Listening...");
    IAsyncResult result = listener.BeginGetContext(new AsyncCallback(DoWork), listener);
    result.AsyncWaitHandle.WaitOne();
    result = null;
}

private void DoWork(IAsyncResult asyncResult)
{
    HttpListener listener = (HttpListener)asyncResult.AsyncState;
    HttpListenerContext context = listener.EndGetContext(asyncResult);
    HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
    // identity has the credentials

}

Edit: I cant change the c# Code anymore. So only nodejs solutions are needed

Edit2: The headers also have no Auth or Authentification property…

Edit3: I cant even find if other location exists except the header for credentials/authentification. But this must be possible right? I mean c# can somehow read this stuff from somewhere…

Any Idea what I can try to find the credentials?



from c# webclient NetworkCredentials in Nodejs

No comments:

Post a Comment