Difference between revisions of "Create login for ASP.Net Web App"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Login Page for ASP.Net Web App)
Line 1: Line 1:
 
=Login Page for ASP.Net Web App=
 
=Login Page for ASP.Net Web App=
Create a new `Razor Page` and call it `login`.
+
Create a new `Razor Page` and call it `login`. In the HTML for the page add the following form to allow the user to enter their username and password:
 
 
In the HTML for the page add the following form to allow the user to enter their username and password:
 
  
 
<syntaxhighlight lang=html>
 
<syntaxhighlight lang=html>
Line 26: Line 24:
 
}
 
}
 
}
 
}
 +
</syntaxhighlight>
 +
 +
Now add a new method to the loginModel class:
 +
 +
<syntaxhighlight lang=c#>
 +
public LocalRedirectResult OnPost()
 +
{
 +
 +
    return LocalRedirect("/login");
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 13:43, 16 October 2024

Login Page for ASP.Net Web App

Create a new `Razor Page` and call it `login`. In the HTML for the page add the following form to allow the user to enter their username and password:

	<h1 class="display-4">Login</h1>

	<form method="post">
		Username:<input type="Text" name="user">
		Password:<input type="password" name="password">
		<input type="submit">
	</form>

Now go to the `loginModel` section (right click the page and select goto PageModel). Inside the loginModel class add the following to handle the database connection:

		string connection = "server=localhost;user=root;database=test;port=3306;password=usbw;";

		public MySqlConnection GetConnection
		{
			get
			{
				return new MySqlConnection(connection);
			}
		}

Now add a new method to the loginModel class:

public LocalRedirectResult OnPost()
{

    return LocalRedirect("/login");
}