Registration
and Login system in
ASP.NET
MVC
Aslam
o Alikum Everyone :)
We
are going to learn how to create a simplest SignUp and SignIn system
in ASP.NET MVC .
For
this, first we create a database to which we store the registration
data. Then we create a signup form and we'll store data from that
form into database. Then using that database, we'll logged into the
system.
So
lets start !
Adding
a Database :
In
Solution Explorer windows, right click on App_data and Add new item
Now
from the wizard, click on “SQL Server Database” and click on
“Add” button.
Now
press Ctrl+Alt+S to open Server explorer window. Expand the Data
Connections and then database1.mdf .Right click on “Tables” and
click on Add new Table
Add
the columns name to the table named “Table” as shown in image
Now
right click on the Id column and click on “Properties”.
From
the properties windows, click and expand “Identity Specification”
and turn it to “True”.
Now
click on “Update” and then “Update Database” to save the
table.
Again
go to Server Explorer Window and Expand Database → right click on
Tables → Show table Data
Now
add some sample data(Email & Password) to the table.
Now
our database is ready. Now we'll create “Context class” against
this database. To do so,Right click on Models in Solution explorer
and Add new item
From
Wizard,select “ADO.NET Entity Data Model” and click Add.
Then
from next window, click on “Generate from Database” and click
Next.
Now
check on “Tables” and click Finish as shown in image.
Now
its time to Code in Home Controller and in Views. Now create a Action
Method and View and named it “Success” which user visit after
successfully Signed Up.
Map
models to the Home controller. For this Add following line in Home
controller
using
YouProjectName.Models;
(You
can see your project Name in top left corner of Visual Studio)
Add
the following Code to the Success Action Method
public
ActionResult
Success( Table
a )
{
Database1Entities
db = new
Database1Entities();
db.Tables.Add(a);
db.SaveChanges();
return
View();
}
Now
create a ActionMethod and View and named it “SignUp” which we use
to store data to database.
Go
to SignUp view and create a SignUp Form there as follows
<h2>SignUp</h2>
<form
action="/Home/Success"
method="get">
Email
:<br>
@Html.Editor("Email")<br>
Password:<br>
@Html.Editor("Password")<br>
<input
type="submit"
value="Submit">
</form>
Now
run the project and go to SignUp page and Insert Data. Click on
Submit button. Success page will be shown.
Now
close the project and in the Server explorer window, Click on Show
Table Data .
Refresh
the table, You'll see the same data there that you entered using
SignUp form.
Its
means our Registration System is Ready now.
Now
we'll create LogIn System.
Create
two new Action Methods and Views and named them “SignIn” and
“SignedIn”.
User
will move to SignedIn page after Successfully Signing In. But if
Email or Password will be incorrect then user will again come back to
the same SignIn page.
Now
Create a SignIn form in SignIn View as follows
<form
action="/Home/SignedIn"
method="get">
Email
:<br>
@Html.Editor("Email")<br>
Password:<br>
@Html.Password("Password")<br>
<input
type="submit"
value="LogIn">
</form>
Add
the following code in SignedIn ActionMethod
public
ActionResult
SignedIn(Table
s)
{
Database1Entities
db = new
Database1Entities();
Table
u = db.Tables.Where(x => x.Email == s.Email &&
x.Password== s.Password).FirstOrDefault();
if(u!=null)
{
return
View();
}
else
{
return
RedirectToAction("SignIn");
}
return
View();
}
Now
Our SignIn System is also
Ready.. Run Project and go to SignIn Page.
Use
any Email and Password that is already stored in our database and
click on LogIn. SignedIn Page will be open.
Now
again use any Email and Password that is not stored in database and
click on LogIn, Again the same page(SignIn) will be shown as we code.
---------------------