简体   繁体   English

C#-服务器端密码保护

[英]C# - Server-side password protection

I am writing two console applications, a client and a server. 我正在编写两个控制台应用程序,一个客户端和一个服务器。 I'm a little stuck at two things, which seemed rather easy at first.. 我在两件事上有些困惑,一开始似乎很容易。

#1: I want to write a function for the following piece of code, that converts bits to a string, but I cant just figure it out. #1: 我想为下面的代码编写一个函数,该函数将位转换为字符串,但是我无法弄清楚。 The server always crashes when I use it. 使用服务器时服务器总是崩溃。 My function is a little bit different than this one, but that's because my current code has to include the connection information, and I think there's a better way to do it: 我的函数与此函数有些不同,但这是因为我当前的代码必须包含连接信息,而且我认为有一种更好的方法:

        byte[] b = new byte[100];
        int k = s.Receive(b);

            string packet = null;
        for (int i = 0; i < k; i++)
        {
            Console.Write(Convert.ToChar(b[i]));
            packet = packet + Convert.ToChar(b[i]);
        }

I guess the function is not the problem, but how I use it is. 我想功能不是问题,但是我怎么用。 Any help would be very much apreciated. 任何帮助将不胜感激。

Edit: I am calling and using it like this: 编辑:我正在调用并像这样使用它:

    byte[] b = new byte[100];
    string response = BitConvert(b);

    if (response == "Hi there")

#2 I want the client to álways send a packet just once, with a password. #2 我希望客户端总是发送一次带有密码的数据包。 And if that password doesn't match the password mentioned as a string in the server, it should close the connection with the client. 并且,如果该密码与服务器中作为字符串提到的密码不匹配,则应关闭与客户端的连接。

I know how to send the packet just once, but I don't know how to check the packet in the server just once for each client. 我知道如何只发送一次数据包,但我不知道如何为每个客户端一次在服务器中检查数据包。

Or in other words, at the moment the server has no way of knowing if the client has already been authenticated. 换句话说,目前服务器无法知道客户端是否已通过身份验证。 So I guess the client needs to have some sort of socket ID, and the server needs a table with the ID, and a boolean to see if it's autenticated or not. 因此,我猜想客户端需要某种套接字ID,而服务器则需要一个具有ID的表和一个布尔值,以查看其是否固定。

The first part, getting the bytes into a string ... how about: 第一部分,将字节变成字符串...怎么样:

byte b[] = new byte[100];
int k = s.Receive(b, b.Length, 0);

string packet = Encoding.ASCII.getString(b, 0, k);

Part 2 ... not sure off the top of my head. 第二部分...不确定我的头顶。

I'm with Rob above on part 1 and as for part 2... Assuming you're using a TCP connection the System.Net.Sockets class should handle your needs. 在第1部分和第2部分中,我与Rob保持联系。假设您使用的是TCP连接,那么System.Net.Sockets类应该可以满足您的需求。

If you use either AcceptSocket or AcceptTcpClient to pull a connection from the incoming connection request queue you'll get a socket or tcpclient that is unique to that connection. 如果使用AcceptSocketAcceptTcpClient从传入的连接请求队列中拉出连接,您将获得该连接所独有的套接字或tcpclient。 simply leave it open until you're done with it. 只需将其保持打开状态,直到完成操作。 (There are also non-blocking alternatives if your service has other things to do...) (如果您的服务还有其他事情要做,那么还有非阻塞替代方法。)

If the client closes it or opens up a new connection the will have to reauthenticate somehow. 如果客户端关闭它或打开新连接,则必须以某种方式重新进行身份验证。 (This may be by including a token that you generate for them when they first authenticate - that's your only option if you're using UDP connections). (这可能是通过添加您在他们首次进行身份验证时为他们生成的令牌-如果您使用的是UDP连接,这是您唯一的选择)。

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

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