This
article will show you how you can create a login form in mvc using entity
framework and allow user to login who are active state in database. This tutorial use c#.net.
Some of my
previous articles are as follows: Simple
Login From in Asp.Net MVC Using C#.Net, Simple
Login Form In Asp.Net Using C#.Net and VB.Net | How to Create Login Form in
Asp.Net Using C#, Ajax
Login Form In Asp.Net Using C#.Net and VB.Net Without PostBack, Ajax
Login Form Validation Without Page Refresh Using jQuery In Asp.Net and C#.Net, Login
Form With LightbBox Effect in Asp.Net, Login
Form By UserType OR User Role In Windows Application In C#.Net , Linq, Shaking
Login Box Open Button Click Using jQuery In Asp.Net.
So for
this article first we will create table
in sql server.
Here is the
table creation query.
GO
/****** Object: Table [dbo].[UserLoginDetail] *****/
SET ANSI_NULLS
ON
GO
SET QUOTED_IDENTIFIER
ON
GO
SET ANSI_PADDING
ON
GO
CREATE TABLE
[dbo].[UserLoginDetail](
[Id]
[int] NOT NULL,
[UserId]
[varchar](50)
NULL,
[Password]
[varchar](50)
NULL,
[StatusId]
[int] NULL,
CONSTRAINT
[PK_UserLoginDEtail] PRIMARY KEY CLUSTERED
(
[Id]
ASC
)WITH (PAD_INDEX =
OFF, STATISTICS_NORECOMPUTE =
OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON
[PRIMARY]
GO
SET ANSI_PADDING
OFF
GO
|
Now we will
create a new mvc application in this application we will add an entity file and
include the user table into the entity file.
.
Now we will
create a model class file. This model class file we will use for passing user
entered detail from view to control on post back.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UserLogin.Models
{
public class UserLoginModel
{
public string UserId { get; set; }
public string Password { get; set; }
public int StatusId { get; set; }
}
}
|
After this
we will add a controller file and add the below code into the controller file.
[HttpGet]
public ActionResult
Index()
{
return View();
}
|
After this
we will create the view. So here is the view code.
@model UserLogin.Models.UserLoginModel
@{
ViewBag.Title
= "Index";
}
@using (Html.BeginForm("Index", "Home"))
{
<table width="100%" border="1" cellspacing="2">
<tr>
<td align="left" colspan="2" ><b>User Login</b></td>
</tr>
<tr>
<td align="right">User
Id :</td>
<td align="left">@Html.TextBoxFor(m => m.UserId)</td>
</tr>
<tr>
<td align="right">Password
:</td>
<td align="left">@Html.TextBoxFor(m => m.Password)</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Login" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
@if (ViewBag.Status == "1")
{
<div style="color:green;font-weight:bold">Successfull login</div>
}
else if (ViewBag.Status == "0")
{
<div style="color:red;font-weight:bold">Wrong User Id and Password!!!</div>
}
</td>
</tr>
</table>
}
|
Now we will add httpPost method . So after adding post method your controller
will look as shown below.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UserLogin.Models;
namespace UserLogin.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
return View();
}
[HttpPost]
public ActionResult
Index(UserLoginModel userLoginModel)
{
DemoEntities
demoEntities = new DemoEntities();
var userLoginStatus = (from data in
demoEntities.UserLoginDetails
where data.UserId ==
userLoginModel.UserId &&
data.Password == userLoginModel.Password &&
data.StatusId == 1
select
data).FirstOrDefault();
if (userLoginStatus!=null)
{
ViewBag.Status = "1";//For successfull login
}else
{
ViewBag.Status = "0";//For unsuccessfull login
}
return View();
}
}
}
|
In above code check the highlighted part of the code. In this I have validated the user id, password and status of the user. It allow to get data only for the active users.
Now we have done run the application to check the output. First we will
try to login for the active user.
Now we will login for the inactive user.
0 comments:
Please let me know your view