Log In Using ASP.NET MVC And WCF SOAP With Entity Framework Using LINQ

This blog includes a step by step tutorial to learn simple authentication functions with the help of ASP.Net MVC and WCF SOAP services using Entity Framework.
 
Create database in sql server:
  1. CREATE TABLE USERS(  
  2.    USERID INT PRIMARY KEY IDENTITY,  
  3.    USERNAME VARCHAR(30),  
  4.    PASSWORD VARCHAR(30)  
 Insert some dummy record into tables
  1. INSERT INTO USERS(USERNAME,PASSWORDVALUES('Kapils','kapil1234')  
  2. INSERT INTO USERS(USERNAME,PASSWORDVALUES('PrashantD','dprashant')  
  3. INSERT INTO USERS(USERNAME,PASSWORDVALUES('MrunalP','patilMrunal123')  
Create a WCF Service project using Visual studio.
 
File ~> New ~>Project,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Project ~> Choose WCF from Left menu and click on WCF service application,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Give a meaningful name for the Project and choose location and click OK.
 
Now we will add entity model to the project.
 
Right click on project solution and select add and select ADO.NET entity data model as shown below.
 
(If you don'd find ADO.Net Entity Model then click on New item, then select data from left menu then select ADO.Net Entity model from main dialogue box)
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Select EF Designer from database option and click next,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Click on New Connection, Connection properties dialogue box will open as shown below.
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Fill in server name. If you select Authentication as SQL Server authentication then enter User Name and Password, Select Database name from Droppdown and click OK. then click Next. Choose entity framework Version and click Next. 
 
Check the checkbox having value as Tables as shown below,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
And click Finish.
 
Now we will create a property class for user login system.
 
We will write our class files into App_Code folder. Initially that folder will not be present. So we need to add it manually.
 
Right click on sloution and select Add, then select Add ASP.Net folder and select App_Code as shown below,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
After adding folder, right click on folder and select New and select class as shown below,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Create a class with name as LogIn.cs and write  the following code into class,
  1. public class LogIn {  
  2.     EntityName ME = new EntityName();  
  3.     private string vusername;  
  4.     private string vpassword;  
  5.     public string Username {  
  6.         get {  
  7.             return vusername;  
  8.         }  
  9.         set {  
  10.             vusername = value;  
  11.         }  
  12.     }  
  13.     public string Password {  
  14.         get {  
  15.             return vpassword;  
  16.         }  
  17.         set {  
  18.             password = value;  
  19.         }  
  20.     }  
  21.     public string UserLogin() {  
  22.         var login = (from user in ME.USERS where user.USERNAME == vusername && user.PASSWORD == vpassword select user).Count() > 0;  
  23.         if (login) {  
  24.             return 1;  
  25.         } else return 0;  
  26.     }  
  27. }  
Open your IService1.cs interface file and remove unwanted code from it and write below lines into IService1.cs
  1. namespace LoginService {  
  2.     [serviceContract]  
  3.     public service IService1 {  
  4.         [OperationContract]  
  5.         string UserLogin(vlogin login);  
  6.     }  
  7.     [DataContract]  
  8.     public class vlogin {  
  9.         private string vusername;  
  10.         private string vpassword;  
  11.         [DataMember]  
  12.         public string UserName {  
  13.             get {  
  14.                 return vusername;  
  15.             }  
  16.             set {  
  17.                 vusername = value;  
  18.             }  
  19.         }  
  20.         public string Password {  
  21.             get {  
  22.                 return vpassword;  
  23.             }  
  24.             set {  
  25.                 vpassword = value;  
  26.             }  
  27.         }  
  28.     }  
  29. }  
Open your Service1.svc.cs file and enter the below code by removing unwanted code from class file.
 
Here you will be unable to access the class file created in your App_Code folder. So add the following line into Service1.svc file
  1. using LoginService.App_Code;  
Then right click on Login.cs file from App_Code folder and click on properties and set Build Action as compiler as shown below.
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Now continue with the coding as shown below. 
  1. namespace LoginService {  
  2.     public class Service1: IService1 {  
  3.         public string UserLogin(vlogin login) {  
  4.             LogIn vlogin = new LogIn();  
  5.             vlogin.Username = login.UserName;  
  6.             vlogin.Password = login.Password;  
  7.             return vlogin.UserLogin();  
  8.         }  
  9.     }  
  10. }  
We have finished with our service part. Now we will see client side coding using MVC 5.
 
Now create a new project in Visual Studio. Select Web from left side menu and click on ASP.Net web application as shown below,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Give Name for application select path and click OK.
 
Select MVC and click OK as shown below.
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Project will be created with default folder structure of ASP.Net mvc.
 
Now run your service project by pressing f5. You will get the following screen. Copy url from the dialogue box.
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Now open WCF project. Right click on References and click on add service reference as shown below.
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Paste the url into address textbox, give name to service reference and press OK as shown below,
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Right click on Controller s folder, select Add and click on Controller, as shown below.
 
Log In Using ASP.NET MVC and WCF SOAP With Entity framework Using LINQ 
 
Select MVC 5 Controller-empty and click OK, give controller name as LoginController.cs. Write the below code into LoginController.cs file
  1. public LoginController: Controller {  
  2.     demoservice.Service1Client ser = new demoservice.Service1Client();  
  3.     demoservice.vlogin vlogin = new demoservice.vlogin();  
  4.     [HttpGet]  
  5.     public ActionResult Login() {  
  6.             return View();  
  7.         }  
  8.         [HttpPost]  
  9.     public ActionResult Login(demoservice.vlogin vlog) {  
  10.         string check = ser.UserLogin(vlog);  
  11.         if (check == "true") {  
  12.             Session["Username"] = vlog.UserName.ToString();  
  13.             return RedirectToAction("User""Home");  
  14.         } else {  
  15.             TempData["error"] = "<script> alert('Incorrect Usrename or Password... Please try again')</script>";  
  16.             return View();  
  17.         }  
  18.     }  
  19. }  
Right click on Action Method Login and select Add View. Don't make any changes and click Add. Login.cshtml file will be created. In Views Folder, under Login folder you will find Login.cshtml file.
 
Add the following code into Login.cshtml,
  1. @{  
  2. ViewBag.Title = "Login";  
  3. }  
  4. @if (TempData["error"] != null)  
  5. {  
  6. @Html.Raw(TempData["error"]);  
  7. }  
  8. @using (Html.BeginForm("Login""Login", FormMethod.Post))  
  9. {  
  10. <fieldset>  
  11.     <legend>Mvc Simple Login Application Demo</legend> @Html.AntiForgeryToken() @Html.ValidationSummary(true) @if (@ViewBag.Message != null) { <div style="border: 1px solid red"> @ViewBag.Message </div> } <table>  
  12.         <tr>  
  13.             <td>@Html.Label("username")</td>  
  14.             <td>@Html.TextBox("username")</td>  
  15.             <td>@Html.ValidationMessage("username")</td>  
  16.         </tr>  
  17.         <tr>  
  18.             <td> @Html.Label("password") </td>  
  19.             <td> @Html.Password("password") </td>  
  20.             <td> @Html.ValidationMessage("password") </td>  
  21.         </tr>  
  22.         <tr>  
  23.             <td></td>  
  24.             <td>  
  25.                 <input type="submit" value="Login" />  
  26.             </td>  
  27.             <td></td>  
  28.         </tr>  
  29.     </table>  
  30. </fieldset>  
  31. }  
Expand Controllers folder and double click on HomeController.cs file and write the following code into HomeController.cs
  1. public ActionResult User()  
  2. {  
  3.    return View();  
  4. }  
Right click on User Action Method, click on Add View and click Add. User.cshtml file will be created. You will find that file into Home folder present Views folder. Write the following code into User.cshtml file.
  1. @{  
  2. ViewBag.Title = "User";  
  3. }  
  4. <fieldset>  
  5.    <legend> User DashBoard </legend>  
  6.       <br />  
  7.       <strong>  
  8.    Welcome @Session["Username"].ToString() </strong>  
  9. </fieldset> 
Now expand Views folder, expand Shared folder, double click on _Layout.cshtml file. In HtmlActionLinks add following line after <li>@Html.ActionLink("Contact", "Contact", "Home")</li> (line no: 25).
  1. <li>@Html.ActionLink("Login""Login""Login")</li>  
Now rebuild the project and check if any error occured. Then click on HomeController.cs file and press f5 button to run the application. (if you have opened a .cshtml file and if you press f5 then automatically that .cshtml file will render first so don't keep any .cshtml files open on screen while excecuting the project).
 
Enter login credentials as per user database data. If username and password is correct then user dashboard will open with welcome message followed by username.
 
If credentials are wrong then an alert will occur.
 
Thank you and happy coding.