Web password

I would like to put a password to my server, someone could help me? plaseeee

thanks! :wink: :wink:

What happened to google? Did it quit working?

Adriansalguero:
I would like to put a password to my server, someone could help me? plaseeee

thanks! :wink: :wink:

How to put a password to your Arduino webserver with Ethernet shield attached:

/*
* Web Server
*
* A simple web server with basic HTTP authentication
*
* Circuit:
* Ethernet shield attached to Arduino board
* Analog inputs attached to pins A0 through A5 (optional)
*/

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 111);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void SendOKpage(EthernetClient &client)
{
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");       
          }
          client.println("</html>");
}


void SendAuthentificationpage(EthernetClient &client)
{
          client.println("HTTP/1.1 401 Authorization Required");
          client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<HTML>  <HEAD>   <TITLE>Error</TITLE>");
          client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}

char linebuf[80];
int charcount=0;
boolean authentificated=false;

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          if (authentificated)
            SendOKpage(client);
          else
            SendAuthentificationpage(client);  
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"cGhwOm5laW4=")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

jurs:
How to put a password to your Arduino webserver with Ethernet shield attached:

/*
  • Web Server
  • A simple web server with basic HTTP authentication
  • Circuit:
  • Ethernet shield attached to Arduino board
  • Analog inputs attached to pins A0 through A5 (optional)
    */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 111);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void SendOKpage(EthernetClient &client)
{
          // send a standard http response header
¬† ¬† ¬† ¬† ¬† client.println(‚ÄúHTTP/1.1 200 OK‚ÄĚ);
¬† ¬† ¬† ¬† ¬† client.println(‚ÄúContent-Type: text/html‚ÄĚ);
¬† ¬† ¬† ¬† ¬† client.println(‚ÄúConnnection: close‚ÄĚ);
          client.println();
          client.println("");
          client.println("");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
¬† ¬† ¬† ¬† ¬† client.println("<meta http-equiv=‚Äúrefresh‚ÄĚ content=‚Äú5‚ÄĚ>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print(“analog input “);
            client.print(analogChannel);
¬† ¬† ¬† ¬† ¬† ¬† client.print(‚ÄĚ is ‚Äú);
            client.print(sensorReading);
¬† ¬† ¬† ¬† ¬† ¬† client.println(‚ÄĚ
“);     
          }
¬† ¬† ¬† ¬† ¬† client.println(‚ÄĚ‚ÄĚ);
}

void SendAuthentificationpage(EthernetClient &client)
{
¬† ¬† ¬† ¬† ¬† client.println(‚ÄúHTTP/1.1 401 Authorization Required‚ÄĚ);
¬† ¬† ¬† ¬† ¬† client.println(‚ÄúWWW-Authenticate: Basic realm=‚ÄúSecure Area‚ÄĚ‚ÄĚ);
¬† ¬† ¬† ¬† ¬† client.println(‚ÄúContent-Type: text/html‚ÄĚ);
¬† ¬† ¬† ¬† ¬† client.println(‚ÄúConnnection: close‚ÄĚ);
          client.println();
          client.println("");
          client.println("    Error");
          client.println("

401 Unauthorized.

");
}

char linebuf[80];
int charcount=0;
boolean authentificated=false;

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
¬† ¬† Serial.println(‚Äúnew client‚ÄĚ);
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        Serial.write(c);
        // if you’ve gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
¬† ¬† ¬† ¬† if (c == ‚Äė\n‚Äô && currentLineIsBlank) {
          if (authentificated)
            SendOKpage(client);
          else
            SendAuthentificationpage(client); 
          break;
        }
¬† ¬† ¬† ¬† if (c == ‚Äė\n‚Äô) {
          // you’re starting a new line
          currentLineIsBlank = true;
¬† ¬† ¬† ¬† ¬† if (strstr(linebuf,‚ÄúAuthorization: Basic‚ÄĚ)>0 && strstr(linebuf,‚ÄúcGhwOm5laW4=‚ÄĚ)>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        }
¬† ¬† ¬† ¬† else if (c != ‚Äė\r‚Äô) {
          // you’ve gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
¬† ¬† Serial.println(‚Äúclient disonnected‚ÄĚ);
  }
}

what is the password :o

Adriansalguero: what is the password :o

You wanted a code with a password. I posted a code with a password. The password is in the code.

Is the password request not working as expected, or what?

jurs: You wanted a code with a password. I posted a code with a password. The password is in the code.

Is the password request not working as expected, or what?

Sorry very much but I don't see the password :/

Correct me if I'm wrong but I believe the password is set to "cGhwOm5laW4=" in his code?

dboy5026:
Correct me if I‚Äôm wrong but I believe the password is set to ‚ÄúcGhwOm5laW4=‚ÄĚ in his code?

ok, and the username? the page gives me error.

Adriansalguero: ok, and the username? the page gives me error.

The code doesn't have a username. You didn't say in your original post that you wanted any username.

Adriansalguero: Sorry very much but I don't see the password :/

You want to use a password, but you are completely clueless about?

So here is a little explanation, the password is checked with this function call:

strstr(linebuf,"cGhwOm5laW4=")>0

So as you might guess, the string "cGhwOm5laW4=" is the hard-coded username and password in the source code.

This username/password string is not encrypted, but encoded in a special way, named "base64".

If you are completely clueless about "base64" string encoding, just go to some online encoder/decoder website of your choice, like https://www.base64decode.org/ Enter the string contents and let it decode.

In the middle of the decoded string there is a double-period ':' which seperates the username from the password. Left is the username, right is the password. You should get the decoded result "php:nein" which means: Username: php Password: nein

If you want to encode a username and password of your own, use the base64 of your choice or go to https://www.base64encode.org/ and let your combination encoded which must be of the form:

username:password

Then place the base64-string in the source code and replace "cGhwOm5laW4=" with your own username and password.

OK?

Sorry but I put this on the page and say error. :disappointed_relieved: :disappointed_relieved:

Adriansalguero: Sorry but I put this on the page and say error. :disappointed_relieved: :disappointed_relieved:

Please define more clearly what you mean by "this", "page" and "error"!

I didn't post you a "page", I posted you an Arduino "sketch" source.

The HTML page is then generated on the fly and will show the analog readings on A0, A1, A2, A3, A4 and A5, after you got the sketch working with your Ethernet shield.

what is the password :o

You've made my day lol

Ok ok okkk! whait I have a server, and I would like to put username and password is not hard!

and I would like to put username and password is not hard!

It certainly seems to be.

How do you expect the Arduino to determine that a given user name and password is legitimate? Or, do you intend to present a login page, and accept any entry as valid?

PaulS: It certainly seems to be.

How do you expect the Arduino to determine that a given user name and password is legitimate? Or, do you intend to present a login page, and accept any entry as valid?

I want to arduino recorded a username and password that is worth me to access to my webserver.

I can show you how to use server cookies to do login authentication but I won’t provide code.

hello topic has been opened for a long time but I have a question. I installed the program works fine. I can easily change the password. when I enter the correct password, I can see the values that arduino reads analog; but I can't add code to turn on a led. How can I burn the led that I added to any digital port and add a button when the password is entered correctly and the page opens. Can you help with this? thank you.