Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7515703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:53:42+00:00 2026-05-30T00:53:42+00:00

My code is querying a server which, if the user and password are correct,

  • 0

My code is querying a server which, if the user and password are correct, returns the “+” character. Otherwise it doesn’t. Bad practice, but I am just using this idea to learn C# and ASP.NET which I am a beginner at (I’ve wrote the program in JSP previously).

My problem :

  • Currently, I seem to have some error which causes my methods to
    always return true. This should not be the case. It should only be
    true when the usersname/pass are authenticated.
  • I want it to display the login form only if the user is not logged in
    and a session has not been started – what is the best way to adapt my
    code to do this?

My code so far :

   @{
    // Initialize general page variables
    string username = "";
    string password = "";
    bool rememberMe = false;

    // Validation
    bool isValid = true;

    // If this is a POST request, validate and process data
        if (IsPost) {
            username = Request.Form["username"];
            password = Request.Form["password"];
            rememberMe = Request.Form["remember"].AsBool();

            // Attempt to login to the external authentication server
            if(isValid){
                using (TcpClient client = new TcpClient("hosty.host.com", 110)) {
                using (NetworkStream stream = client.GetStream()) {
                using (StreamReader reader = new StreamReader(stream)) {
                using (StreamWriter writer = new StreamWriter(stream)) {
                    writer.WriteLine("USER " + username );
                    writer.WriteLine("PASS " + password );
                    string response = reader.ReadLine();
                    isValid = response[ 0 ] == '+';
                    Response.Write(response);
                    writer.WriteLine("quit\n");
                }
                }
                }
                }
            }

            if (isValid) {
                <text>IT WORKED---></text>
            //USER LOGGED IN/ SESSION STARTED


            } else {
                <text>IT DIDNT WORK :( </text>
                //USER NOT LOGGED IN, SESSION NOT STARTED
            }
        }
    }   
        <h2>Login Here</h2>
        <form action="" method="post">
          <fieldset>
            <legend>Login Form</legend>
            <label for="username">Username:
              <input type="text" name="username" id="username" value="" />
            </label>
            <label for="password">Password:
              <input type="password" name="password" id="password" value="" />
            </label>
            <label for="remember">
              <input class="checkbox" type="checkbox" name="remember" id="remember" checked="checked" />
              Remember me</label>
            <p>
              <input type="submit" name="login" id="login" value="Login" />
              &nbsp;
              <input type="reset" name="reset" id="reset" value="Reset" />
            </p>
          </fieldset>
        </form>
      </div>
          </fieldset>
        </form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:53:45+00:00Added an answer on May 30, 2026 at 12:53 am

    You start with isValid=true but if your program can not even be able to connect to the server the isValid is remain true.

    So maybe your users not even validate at all, you start with isValid and end with the same even if the validation is not done.

    The code as it is, I will start it with false, and remove one line.

    // Validation
        bool isValid = false;
    
        // If this is a POST request, validate and process data
            if (IsPost) {
                username = Request.Form["username"];
                password = Request.Form["password"];
                rememberMe = Request.Form["remember"].AsBool();
    
                // Attempt to login to the external authentication server
                // if(isValid)
                {
                    using (TcpClient client = new TcpClient("hosty.host.com", 110)) {
                    using (NetworkStream stream = client.GetStream()) {
                    using (StreamReader reader = new StreamReader(stream)) {
                    using (StreamWriter writer = new StreamWriter(stream)) {
                        writer.WriteLine("USER " + username );
                        writer.WriteLine("PASS " + password );
                        string response = reader.ReadLine();
                        isValid = response[ 0 ] == '+';
                        Response.Write(response);
                        writer.WriteLine("quit\n");
                    }
                    }
                    }
                    }
                }
    
                if (isValid) {
                    <text>IT WORKED---></text>
                //USER LOGGED IN/ SESSION STARTED
    
    
                } else {
                    <text>IT DIDNT WORK :( </text>
                    //USER NOT LOGGED IN, SESSION NOT STARTED
                }
            }
    

    Also be ware that Page.IsValid is an existing parameter on web form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am querying information from Active Directory . I have code that works, but
I have a python script which is querying a MySQL server on a shared
I'm writing a server querying tool, and I have a little bit of code
OK, so there is a page I'm querying on another server that returns a
I am connecting to a server from an android device and querying the database
I have a ListView which I filled in from SQL Server view called view_ListaKlientow
I have a lot of code I am trying to run where I'm querying
Scenario: I'm running code on the client that connects to a server and uses
I have a custom LDAP schema installed on my OpenLDAP server which is as
I connect to a SAP server via SAP Logon Control TLB (which eats about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.