connectseekers.com

Friday, 17 June 2016

Login From With Active User Validation in Asp.Net MVC Using C#.Net

6/17/2016 - By Pranav Singh 0

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.


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.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top