Asp Net Save Login Information Without Database Updated FREE

Asp Net Save Login Information Without Database

This article explains the following:

  • How to create a ASP.Net MVC Project
  • How to Add ADO.Cyberspace Entity Data Model
  • How to Add Controller
  • How to validate User Credentials
  • How to keep User Details in Sessions and display in User DashBoard

Pace: Design your Database

Create the UserProfile table using the following script.

  1. Create Table UserProfile
  2.     (
  3.         UserIdint  primary key identity(ane, 1),
  4.         UserName varchar(l),
  5.         Password varchar(50),
  6.         IsActive fleck
  7.     )

Insert user records using the following script.

  1. Insert into UserProfile
  2. Select'jaipal' , 'jai1234' , 1 Union All
  3. Select'praveen' , 'praveen1234' , i Union All
  4. Select'pruthvi' , 'pruthvi1234' , 1

Step one: Create Project

Go to FILE, New, so click on Project.

create project

Select Visual C#, Spider web under Installed templates. Later that select ASP.Internet MVC iv Spider web Application, and then mention the Application Name (MvcLoginAppDemo) and Solution Name as you wish, then click OK.

ASP.NET MVC 4 Web Application

Under Project template select a template every bit Basic, then view engine as Razor. Click OK.

select a template as Basic

Footstep 2: Add together Entity Information Model

Get to Solution Explorer, Right Click on Project, Add, then select ADO.NET Entity Data Model.

select ADO.NET Entity Data Model

Requite information technology a meaningful model proper noun and then click on Add together.

sampleDataModel

Select Generate from database and then click on Next.

Generate from database

Click on New Connexion,

New Connection

After clicking on New Connection, nosotros have to provide the following Connexion Backdrop in the following magician.

  • Provide the Server proper name.
  • Select the "Use SQL Server Authentication" radio button.
  • Enter the User name and Password in the password text box.
  • Check the "Save my password" checkbox.
  • Select the "Select or enter a database proper name:" radio button.
  • Select the database to which you lot desire to set the connexion.
  • Click on the "Exam Connection" button to ensure the connection can be established.
  • Then click OK.

Test Connection

Select radio button: Yes include the sensitive data in the connection cord.

Choose your data connection

Choose your database objects, equally in the following image.

Choose your database objects

 Click on End. At this signal UserProfie entity volition be created.

UserProfie

Step three: Add a Controller

Get to Solution Explorer, Right click on Controller binder, Add together and so click on Controller.

( Or ) Simply apply shortcut key Ctrl + Yard, Ctrl + C,

Add a Controller

Provide the Controller Proper name, and Scaffolding template every bit Empty MVC Controller. Then click on Add.

Empty MVC Controller

Write the post-obit code in HomeController.

  1. using  System.Linq;
  2. using  Organization.Web.Mvc;
  3. namespace  MvcLoginAppDemo.Controllers
  4. {
  5. public class  HomeController: Controller
  6.     {
  7. public  ActionResult Login()
  8.         {
  9. return  View();
  10.         }
  11.         [HttpPost]
  12.         [ValidateAntiForgeryToken]
  13. public  ActionResult Login(UserProfile objUser)
  14.         {
  15. if  (ModelState.IsValid)
  16.             {
  17. using (DB_Entities db = new  DB_Entities())
  18.                 {
  19.                     var obj = db.UserProfiles.Where(a => a.UserName.Equals(objUser.UserName) && a.Password.Equals(objUser.Password)).FirstOrDefault();
  20. if  (obj != nothing )
  21.                     {
  22.                         Session["UserID" ] = obj.UserId.ToString();
  23.                         Session["UserName" ] = obj.UserName.ToString();
  24. return  RedirectToAction( "UserDashBoard" );
  25.                     }
  26.                 }
  27.             }
  28. render  View(objUser);
  29.         }
  30. public  ActionResult UserDashBoard()
  31.         {
  32. if  (Session[ "UserID" ] != null )
  33.             {
  34. render  View();
  35.             }else
  36.             {
  37. return  RedirectToAction( "Login" );
  38.             }
  39.         }
  40.     }
  41. }

Pace four: Create Views

Create View for Login Action Method

Right click on the Login Activeness method, and then click on Add View as in the following picture.

Create Views

Create Strongly Typed View

  • View Proper noun must be an action method name.
  • Select view engine as Razor.
  • Select Create a strongly typed view CheckBox .
  • Select Model class as UserProfile (MvcLoginAppDemo)
  • Select Scaffold template equally Empty
  • Click on Add

add view

And write the following lawmaking in Login.cshtml (view).

  1. @model MvcLoginAppDemo.UserProfile
  2. @{
  3. ViewBag.Title ="Login" ;
  4. }
  5. @using  (Html.BeginForm( "Login" , "Habitation" , FormMethod.Mail service))
  6. {
  7. <fieldset>
  8. <legend>Mvc Uncomplicated Login Application Demo</legend>
  9. @Html.AntiForgeryToken()
  10. @Html.ValidationSummary(true )
  11. @if  (@ViewBag.Message != zero )
  12. {
  13. <div mode="border: 1px solid red" >
  14. @ViewBag.Message
  15. </div>
  16. }
  17. <table>
  18. <tr>
  19. <td>@Html.LabelFor(a => a.UserName)</td>
  20. <td>@Html.TextBoxFor(a => a.UserName)</td>
  21. <td>@Html.ValidationMessageFor(a => a.UserName)</td>
  22. </tr>
  23. <tr>
  24. <td>
  25. @Html.LabelFor(a => a.Password)
  26. </td>
  27. <td>
  28. @Html.PasswordFor(a => a.Password)
  29. </td>
  30. <td>
  31. @Html.ValidationMessageFor(a => a.Password)
  32. </td>
  33. </tr>
  34. <tr>
  35. <td></td>
  36. <td>
  37. <input blazon="submit"  value= "Login"  />
  38. </td>
  39. <td></td>
  40. </tr>
  41. </table>
  42. </fieldset>
  43. }

Create View for UserDashBoard Action method aforementioned every bit login view. And write the following code in UserDashBoard.cshtml (View).

  1. @ {
  2.     ViewBag.Title ="UserDashBoard" ;
  3. }
  4. < fieldset >
  5.     < legend > User DashBoard < /legend>
  6. @if (Session[ "UserName" ] != null ) { < text >
  7.         Welcome @Session["UserName" ].ToString() < /text>
  8. } < /fieldset>

Step 5: Prepare as StartUp Page

Go to Solution Explorer, Projection, App_Start, and then RouteConfig.cs and change the action name from Index to Login (Login.cshtml as start up folio).

Set as StartUp Page

Pace 6: Run the Application

Run the Application

Provide the user credentials and click on OK. If you provide the valid user credentials then the user name will be displayed on your dashboard.

user dashboard

I promise you enjoyed it.

Asp Net Save Login Information Without Database

DOWNLOAD HERE

Source: https://www.c-sharpcorner.com/article/simple-login-application-using-Asp-Net-mvc/

Posted by: danieldiumen.blogspot.com

Comments