connectseekers.com

Wednesday, 25 June 2014

Simple Login From in Asp.Net MVC Using C#.Net

6/25/2014 - By Pranav Singh 1

In this article I will show you how you can create a login form you mvc application by validating user from data base. In this I have used MVC3, C#.Net and EntityFrameWork .This code is compatible with MVC2/MVC3/MVC4/MVC5. 


So for this article first we will create a new mvc application. After this we will create a UserDetail table in sql DB. This table we will use in out entity file.

Field Name
Data Type
Id
Int
UserId
Varchar(50)
Password
Varchar(50)

Now we will add mode class as shown below.

Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCDemo.Models
{
    public class UserLoginModel
    {
        public string UserId { get; set; }
        public string Password { get; set; }
    }
}

After creating model we will create a controller class in controller folder, and add the below code.

Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;

namespace MVCDemo.Controllers
{
    public class UserLoginController : Controller
    {
        //
        // GET: /UserLogin/

        public ActionResult Index()
        {
            UserLoginModel _objuserloginmodel = new UserLoginModel();
            return View(_objuserloginmodel);
        }
        [HttpPost]
        public ActionResult Index(UserLoginModel _objuserloginmodel)
        {
            /*Create instance of entity model*/
            NorthwindEntities objentity = new NorthwindEntities();
            /*Getting data from database for user validation*/
            var _objuserdetail = (from data in objentity.UserDetails
                                  where data.UserId == _objuserloginmodel.UserId
                                  && data.Password == _objuserloginmodel.Password
                                  select data);
            if (_objuserdetail.Count() > 0)
            {
                /*Redirect user to success apge after successfull login*/
                ViewBag.Message = 1;
            }
            else
            {
                ViewBag.Message = 0;
            }
            return View(objuserloginmodel);
        }
    }
}

In above code we have defined the httpget and httppost method in this httpget method is used for firing page load events and httppost when the we click on submit button to raise the button click event. In this method we will validating the user from data base by using linq query.

After completing controller code we will create view for the controller action.

View
@model MVCDemo.Models.UserLoginModel
@{
    ViewBag.Title = "Simple Login From in Asp.Net MVC Using C#.Net";
}
<h2>
    Simple Login From in MVC</h2>

@using (@Html.BeginForm("Index", "UserLogin"))
{
<table width="25%" style="border: 1 solid back; border-collapse: collapse;" border="1" cellpadding="5" cellspacing="5">
    <tr>
        <td colspan="2" style="background-color: Gray;">LOGIN
        </td>
    </tr>
    <tr>
        <td align="right"> User Id:</td>
        <td align="left">@Html.TextBoxFor(m => m.UserId, new { @id = "txtuserid",@style="width:200px;" })</td>
    </tr>
    <tr>
        <td align="right">Password:</td>
        <td align="left">@Html.PasswordFor(m => m.Password, new { @id = "txtpasword", @style = "width:200px;" }) </td>
    </tr>
    <tr>
        <td align="center" colspan="2">
            <input type="submit" value="Login" />
        </td>
    </tr>
</table>
    if (ViewBag.Message == 1)
    {
    <script language="javascript">
        alert("Successfull login.");
    </script>
    }
    if (ViewBag.Message == 0)
    {
    <script language="javascript">
        alert("Please provide correct userid or password.");
    </script>
    }
}

Now run the application


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

1 comment:

  1. periya tom cruse ne velaya paru da

    ReplyDelete

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