简体   繁体   English

Nodejs 中的 c# webclient NetworkCredentials

[英]c# webclient NetworkCredentials in Nodejs

I have a c# .net Client with this Code:我有一个 c# .net 客户端,代码如下:

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 服务器。 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#:这就是我可以访问 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.编辑:我不能再更改 c# 代码了。 So only nodejs solutions are needed所以只需要nodejs解决方案

Edit2: The headers also have no Auth or Authentification property… Edit2:标头也没有 Auth 或 Authentification 属性...

Edit3: I cant even find if other location exists except the header for credentials/authentification. Edit3:我什至找不到除了 header 用于凭据/身份验证之外是否存在其他位置。 But this must be possible right?但这一定是可能的吧? I mean c# can somehow read this stuff from somewhere…我的意思是 c# 可以以某种方式从某个地方读取这些内容……

Any Idea what I can try to find the credentials?任何想法我可以尝试找到凭据?

To make your C# client to send its networkCredentials as HTTP Basic Authentication to your Nodejs server;使您的 C# 客户端将其 networkCredentials 作为 HTTP 基本身份验证发送到您的 Nodejs 服务器; the server should return a response whose header contains a HTTP 401 Unauthorized status and a WWW-Authenticate field if the request does not contain the Authorization header.如果请求不包含授权 Z099FB995346F31C749F6E40DB0F395E,服务器应返回其 header 包含 HTTP 401 未授权状态和 WWW-Authenticate 字段的响应。 This will cause your C# client retry the POST with Authorization header.这将导致您的 C# 客户端使用授权 header 重试 POST。

This process it is called Authentication challenge in case you want to search for more info.如果您想搜索更多信息,此过程称为身份验证质询

There are serveral packages that does that for you;有几个软件包可以为您做到这一点; like http-auth or you can code it by hand (it is not very hard as it is just a matter of checking the existence of the Authorization header in the request and, if there is none or incorrect credentials, make a 401 response with a WWW-Authenticate field)http-auth或者你可以手动编码(这不是很难,因为它只是检查请求中是否存在授权 header ,如果没有或不正确的凭据,使用 401 响应WWW-认证字段)

ie from the top of my head:即从我的头顶:

var http = require('http');

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

console.log('started');

function listener(req, res) {

  if (!req.headers.authorization) {
    res.statusCode = 401;
    res.statusMessage = 'Unauthorized';
    res.setHeader('WWW-Authenticate', 'Basic');
    res.end();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM