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:
- CREATE TABLE USERS(
- USERID INT PRIMARY KEY IDENTITY,
- USERNAME VARCHAR(30),
- PASSWORD VARCHAR(30)
Insert some dummy record into tables
- INSERT INTO USERS(USERNAME,PASSWORD) VALUES('Kapils','kapil1234')
- INSERT INTO USERS(USERNAME,PASSWORD) VALUES('PrashantD','dprashant')
- INSERT INTO USERS(USERNAME,PASSWORD) VALUES('MrunalP','patilMrunal123')
Create a WCF Service project using Visual studio.
File ~> New ~>Project,
Project ~> Choose WCF from Left menu and click on WCF service application,
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)
Select EF Designer from database option and click next,
Click on New Connection, Connection properties dialogue box will open as shown below.
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,
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,
After adding folder, right click on folder and select New and select class as shown below,
Create a class with name as LogIn.cs and write the following code into class,
- public class LogIn {
- EntityName ME = new EntityName();
- private string vusername;
- private string vpassword;
- public string Username {
- get {
- return vusername;
- }
- set {
- vusername = value;
- }
- }
- public string Password {
- get {
- return vpassword;
- }
- set {
- password = value;
- }
- }
- public string UserLogin() {
- var login = (from user in ME.USERS where user.USERNAME == vusername && user.PASSWORD == vpassword select user).Count() > 0;
- if (login) {
- return 1;
- } else return 0;
- }
- }
Open your IService1.cs interface file and remove unwanted code from it and write below lines into IService1.cs
- namespace LoginService {
- [serviceContract]
- public service IService1 {
- [OperationContract]
- string UserLogin(vlogin login);
- }
- [DataContract]
- public class vlogin {
- private string vusername;
- private string vpassword;
- [DataMember]
- public string UserName {
- get {
- return vusername;
- }
- set {
- vusername = value;
- }
- }
- public string Password {
- get {
- return vpassword;
- }
- set {
- vpassword = value;
- }
- }
- }
- }
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
- 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.
Now continue with the coding as shown below.
- namespace LoginService {
- public class Service1: IService1 {
- public string UserLogin(vlogin login) {
- LogIn vlogin = new LogIn();
- vlogin.Username = login.UserName;
- vlogin.Password = login.Password;
- return vlogin.UserLogin();
- }
- }
- }
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,
Give Name for application select path and click OK.
Select MVC and click OK as shown below.
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.
Now open WCF project. Right click on References and click on add service reference as shown below.
Paste the url into address textbox, give name to service reference and press OK as shown below,
Right click on Controller s folder, select Add and click on Controller, as shown below.
Select MVC 5 Controller-empty and click OK, give controller name as LoginController.cs. Write the below code into LoginController.cs file
- public LoginController: Controller {
- demoservice.Service1Client ser = new demoservice.Service1Client();
- demoservice.vlogin vlogin = new demoservice.vlogin();
- [HttpGet]
- public ActionResult Login() {
- return View();
- }
- [HttpPost]
- public ActionResult Login(demoservice.vlogin vlog) {
- string check = ser.UserLogin(vlog);
- if (check == "true") {
- Session["Username"] = vlog.UserName.ToString();
- return RedirectToAction("User", "Home");
- } else {
- TempData["error"] = "<script> alert('Incorrect Usrename or Password... Please try again')</script>";
- return View();
- }
- }
- }
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,
- @{
- ViewBag.Title = "Login";
- }
- @if (TempData["error"] != null)
- {
- @Html.Raw(TempData["error"]);
- }
- @using (Html.BeginForm("Login", "Login", FormMethod.Post))
- {
- <fieldset>
- <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>
- <tr>
- <td>@Html.Label("username")</td>
- <td>@Html.TextBox("username")</td>
- <td>@Html.ValidationMessage("username")</td>
- </tr>
- <tr>
- <td> @Html.Label("password") </td>
- <td> @Html.Password("password") </td>
- <td> @Html.ValidationMessage("password") </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <input type="submit" value="Login" />
- </td>
- <td></td>
- </tr>
- </table>
- </fieldset>
- }
Expand Controllers folder and double click on HomeController.cs file and write the following code into HomeController.cs
- public ActionResult User()
- {
- return View();
- }
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.
- @{
- ViewBag.Title = "User";
- }
- <fieldset>
- <legend> User DashBoard </legend>
- <br />
- <strong>
- Welcome @Session["Username"].ToString() </strong>
- </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).
- <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.