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.
- Create Table UserProfile
- (
- UserIdint primary key identity(ane, 1),
- UserName varchar(l),
- Password varchar(50),
- IsActive fleck
- )
Insert user records using the following script.
- Insert into UserProfile
- Select'jaipal' , 'jai1234' , 1 Union All
- Select'praveen' , 'praveen1234' , i Union All
- Select'pruthvi' , 'pruthvi1234' , 1
Step one: Create Project
Go to FILE, New, so click on 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.
Under Project template select a template every bit Basic, then view engine as Razor. Click OK.
Footstep 2: Add together Entity Information Model
Get to Solution Explorer, Right Click on Project, Add, then select ADO.NET Entity Data Model.
Requite information technology a meaningful model proper noun and then click on Add together.
Select Generate from database and then click on Next.
Click on New Connexion,
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.
Select radio button: Yes include the sensitive data in the connection cord.
Choose your database objects, equally in the following image.
Click on End. At this signal UserProfie entity volition be created.
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,
Provide the Controller Proper name, and Scaffolding template every bit Empty MVC Controller. Then click on Add.
Write the post-obit code in HomeController.
- using System.Linq;
- using Organization.Web.Mvc;
- namespace MvcLoginAppDemo.Controllers
- {
- public class HomeController: Controller
- {
- public ActionResult Login()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Login(UserProfile objUser)
- {
- if (ModelState.IsValid)
- {
- using (DB_Entities db = new DB_Entities())
- {
- var obj = db.UserProfiles.Where(a => a.UserName.Equals(objUser.UserName) && a.Password.Equals(objUser.Password)).FirstOrDefault();
- if (obj != nothing )
- {
- Session["UserID" ] = obj.UserId.ToString();
- Session["UserName" ] = obj.UserName.ToString();
- return RedirectToAction( "UserDashBoard" );
- }
- }
- }
- render View(objUser);
- }
- public ActionResult UserDashBoard()
- {
- if (Session[ "UserID" ] != null )
- {
- render View();
- }else
- {
- return RedirectToAction( "Login" );
- }
- }
- }
- }
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 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
And write the following lawmaking in Login.cshtml (view).
- @model MvcLoginAppDemo.UserProfile
- @{
- ViewBag.Title ="Login" ;
- }
- @using (Html.BeginForm( "Login" , "Habitation" , FormMethod.Mail service))
- {
- <fieldset>
- <legend>Mvc Uncomplicated Login Application Demo</legend>
- @Html.AntiForgeryToken()
- @Html.ValidationSummary(true )
- @if (@ViewBag.Message != zero )
- {
- <div mode="border: 1px solid red" >
- @ViewBag.Message
- </div>
- }
- <table>
- <tr>
- <td>@Html.LabelFor(a => a.UserName)</td>
- <td>@Html.TextBoxFor(a => a.UserName)</td>
- <td>@Html.ValidationMessageFor(a => a.UserName)</td>
- </tr>
- <tr>
- <td>
- @Html.LabelFor(a => a.Password)
- </td>
- <td>
- @Html.PasswordFor(a => a.Password)
- </td>
- <td>
- @Html.ValidationMessageFor(a => a.Password)
- </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <input blazon="submit" value= "Login" />
- </td>
- <td></td>
- </tr>
- </table>
- </fieldset>
- }
Create View for UserDashBoard Action method aforementioned every bit login view. And write the following code in UserDashBoard.cshtml (View).
- @ {
- ViewBag.Title ="UserDashBoard" ;
- }
- < fieldset >
- < legend > User DashBoard < /legend>
- @if (Session[ "UserName" ] != null ) { < text >
- Welcome @Session["UserName" ].ToString() < /text>
- } < /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).
Pace 6: 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.
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
Post a Comment