简体   繁体   中英

How can I connect front-end with back-end?

I have a web page (front-end) created with ASP.NET (visual studio) on one laptop and a server (back-end) for my web page on another laptop. In the first place, I have a login page and I want to take the username and the password from textBox-es (front-end) and somehow transfer the data to the other laptop to verify if it's correct and then receive if the data it's correct or not. The front-end is on Windows and the server is on Linux. My teammate is using python on back-end.

I'm working with HTML, CSS and a little bit of C# on the front-end and I don't know how to transfer that username and password to my teammate and how to receive an answer from him. He has the database on his laptop and he's the one who will verify if the pair is correct or not and send me an answer, also he wants to code in python.

We think that we can do some magic connection between ASP.NET and Node.js.

How do I do this data transfer?

It's my first time doing this so please be gentle because I really don't know what to do.

Your question is quite broad and while I am not that fluent in web applications I will share what advice I can. Because the question is broad, you cannot get an exact, short and concise, answer.

Some clarifications and feedback regarding your question.

I have a web page (front-end) created with ASP.NET...

You actually have a web application here (that, in your case, includes one, or more, web pages). Give more attention to such little details, programming in itself is a form of grammar natzy-ism but it's not grammar and you are not being corrected by a person, but by machines and life. A nice definition of web application: https://blog.stackpath.com/web-application/

We think that we can do some magic connection between ASP.NET and Node.js.

I am not specialized in web application but it appears to me that this line comes completely out of the blue, you have not indicated that you are using or intend to use Node.js in your back-end; you indicated that you were using Python, so I will ignore this statement. Oh and there is no magic, there are only race conditions.

Structurally speaking , I would split this question into several smaller ones. Some suggestions follow.

Because you are attempting a somewhat more complicated task you cannot escape de idea of a design pattern (especially because of using ASP.NET which is designed to be patterning friendly).

So your first question should be similar to

how to communicate with a web server asp.net

Another possible way of how to-ing would be

how to transmit data to server C#

From you explication I presume that you already have implemented the UI forms that will retrieve the input data so I will skip UI related questions.

Since you wish to send user SENSITIVE data over the internet then you MUST think about security. Further questions

how to store password in database securely

  1. this question will actually lead you to examples of transmitting the password and maybe even receiving the content

There are 2 more issues I wish to discuss.

  1. While searching the internet for tutorials you will probably see a lot working with client and server applications on the same machine. Those are still valid tutorials for you. Read them! I will explain further down why it's (almost) the same for you even if your case is a remote server.

  2. You need to somehow be able to test your application.

Addressing #1

Why working with localhost (127.0.0.1) is for you the same as working with a real server (in your case).

Let's say you finish the application, the back-end is also made and you wish to communicate with one another. You read on the internet that you need to known the IP of the server so you ask your partner to for him to give his IP. You search the wild wild web and reach the conclusion that to find your IP you would try:

  • executing ipconfig.exe (or what tool you use depending on your platform) and getting the IPv4 Address. . . . . . . . . . . : 192.168.1.12 IPv4 Address. . . . . . . . . . . : 192.168.1.12 IPv4 Address. . . . . . . . . . . : 192.168.1.12 value. you would presume that that is the IP you need to put in your application to connect to. This is FALSE .

  • reaching the conclusion that that does not work, you find that you can get your IP with online sites such as https://whatismyipaddress.com/ and use that IP for your server address. This (in 99% cases) is FALSE.

A bit of poetry

Consider IP as you address in the world and your local (private) IP as the exact place in your house. The IP address you see there is a local IP and it would indicate in which chair you are sitting in, let's say the 12 from the already provided example. Anyone in the world (who has 12 chairs anyway) can sit in the 12th chair in their house. You cannot use this information to identify the address of someone.

How would you know that that IP is a local IP?

One way would be to look at the the full output of ipconfig.exe (or whatever tool you are using) where you see a Wireless LAN adapter Wi-Fi: above from where you got your IP, the LAN there ( hxxps://en.wikipedia.org/wiki/Local_area_network ) is an indicator of that.

The other (more straightforward) is by recognizing the IP range (or visual pattern, if IP range is not a clear concept for you just yet). If it starts with 198.168. then it's a local/private IP address. Please read here more: https://en.wikipedia.org/wiki/Private_network

IPs are a valuable commodity (I joke you not). As the world has evolved you cannot now simply plug your computer with an internet cable, and voila you have your own IP. This was (relatively) true when the internet first started decades ago but now the IPs are managed by big (huge) companies that they allocate them as they consider.

The IP address space is managed globally by the Internet Assigned Numbers Authority (IANA), and by five regional Internet registries (RIRs) responsible in their designated territories for assignment to local Internet registries, such as Internet service providers, and other end users.

Wiki quote: https://en.wikipedia.org/wiki/IP_address

I also highly (please do) recommend you read the following answer hxxps://superuser.com/questions/323801/how-can-i-own-an-ip-address

If you ask your (real world) neighbors to look up https://whatismyipaddress.com/ (and they have the same ISP and monthly plan as you) there is a strong possibility that they will see the same IP as you see. To put it simply, the IP you see there, when accessing sites such as whatismyipaddress, is the IP that that your ISP has assigned to the network node you are in. It would be a waste of IPs to assign each user an IP (not to mention there actually aren't mathematically that many IPs to do so).

Regarding your current predicament. I can see 3 options.

Option 1 - buy hosting

If your response is "I really really wanna have a separate IP" (or domain) then you will need to buy from a hosting service an IP (the good kind) that will point to a device the hosting company has. You usually buy domains that point to IPs, but let's not got there for now. You will connect to that machine/devices and install your code on the server there. Then, and only then, you could get the IP/domain of that machine and use it in your code and it will work (this is the 1% cases where sites like whatismyipaddress will actually help you).

Recommended searches: server hosting cheap:D (or something free for students if you are students)

Option 2 - work locally

If your project is used for academia and there is no actual need for a separate server then you and your partner will run the client/server applications while connected to the same wireless modem. Ask him to get his IP in this way (here you actually need the LAN IP), you hardcoded it in your code (preferably a separate config file, this way no extra compilation is required) and voila, it works.

Option 3 - LAN over the internet

Technically this would be hard to describe but the idea is you use programs that communicate over the internet, you each install it on your computer, somehow connect with each other (usually a name/password pair) and that software make is seem for the programs on your computer that you are actually in a LAN setup (Option 2) . In my days I used hamachi so that we could play CS online:D.

  • hxxps://www.howtogeek.com/172762/how-to-play-old-lan-games-over-the-internet/

Regarding #2

Until your partner provides a server you will need to test your code. So you will need to somehow fake a server response so that you can actually continue development.

Quite simply, the operation of faking something, a return/a class, in your code, so that you can correctly develop and test your code is known as mocking

how to mock server response asp.net

  • hxxps://gingter.org/2018/07/26/how-to-mock-httpclient-in-your-net-c-unit-tests/

For beginning programmers I think this is one of the most complicated parts. The concept of mocks and tests, while they do appear simple, are usually not engraved into the mind of young, innocent, previously untainted, future programmers. They are left stuck with printfs until graduations (even a dose of hate towards tests is sprinkled in the mix, what a pity).

As a courtesy, I wish to further extend that madness. Please, for all that is good and sacred in this world, do not use this code in production, burn it after you used it to understand some of the basics, BURN IT!

I modified the code from here Python 3.x BaseHTTPServer or http.server so that you could run a local HTTP server on your machine and test your application. You simply run it (python3) and test the sending of user name and password to the server using the following pagan syntax:

127.0.0.1:4242/?user=aba&password=Aba1 which would return I'al cohol you later

#!/usr/bin/python
from http.server import BaseHTTPRequestHandler,HTTPServer
from urllib.parse import urlparse, parse_qs
HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT_NUMBER = 4242  # Port to listen on (non-privileged ports are > 1023)

# !!!!!!!!!!!!!!! THIS IS ONLY FOR DIDACTICAL PURPOSE< NEVER EVER DO SOMETHING LIKE THIS IN PRODUCTION!!!!!!!
# NEVER EVER STORE PASSWORDS LIKE THIS !!!!!!!
# LITERRLAY NEVER!!!!!!
data_base = [
    {
        'user': 'ionzapada',
        'password': 'neaomat',
        'data': b"I like pie!"
    },
    {
        'user': 'aba',
        'password': 'Aba1',
        'data': b"I'al cohol you later"
    }
]

class myHandler(BaseHTTPRequestHandler):

    #Handler for the GET requests
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message

        query_components = parse_qs(urlparse(self.path).query)
        # DO NOT USE THIS LOGIC ON ANY PRODUCTION PLATFORM!!!!
        # NEVER SEND PASSWORDS THIS WAY!!! 
        # NEVER SEND PASSWORDS!!!! 
        # NEVER SEND HASHES WITHOUT SALT!!!! 
        # NEVER IMPLEMENT YOUR OWN TRICKY ALGORITHM!!!!
        user_name = query_components.get("user",[None])[0]
        password = query_components.get("password",[None])[0]
        if user_name and password:
            for data_base_entry in data_base:
                if data_base_entry['user'] == user_name:
                    print('Found searched user: {}'.format(user_name))
                    if data_base_entry['password'] == password:
                        print('Valid password for user {} given; returning db data'.format(user_name))
                        self.wfile.write(data_base_entry['data'])
                    else:
                        self.wfile.write(b"Not supported!")
        else:
            self.wfile.write(b"Not supported!")        
        return

try:
    server = HTTPServer((HOST, PORT_NUMBER), myHandler)
    print('Started httpserver on port', PORT_NUMBER)
    server.serve_forever()

except KeyboardInterrupt:
    print('CTRL+C received, shutting down the web server')
    server.socket.close()

I burned my finger writing that, do not ever send user names and passwords in this way!!! DO NOT EVEN SEND PASSWORDS FOR THE LOVE OF GOD!!!!!!!! search for standard and secure ways of doing this!!!!

Please also visit the links indicated by @amc they are an also good start.

PS I am not allowed to have more then 8 link in one post, on some links that I have posted change the hxxp to http for them to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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