ASP.Net Web Application Using WCF

Introduction

This article shows how to create an ASP.Net Web Application for login using WCF. Whenever a valid employee tries to login into the application the service will revert the employee id to the application if the input credentials are correct.

Database Structure

  1. create table tbl_emp(  
  2. u_name nvarchar(50),  
  3. u_pass nvarchar(50),  
  4. u_empid nvarchar(50),)  
  1. insert into tbl_emp values('useru','passu','101')  
  2. insert into tbl_emp values('user','pass','u001')  
  3. insert into tbl_emp values('Shubham','12@!','u002')  
  4. insert into tbl_emp values('Atul','1234','u003')  


Now open Visual Studio for creating a WCF Service.





IService1.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.   
  9. namespace WcfServiceLogin  
  10. {  
  11.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.  
  12.     [ServiceContract]  
  13.     public interface IService1  
  14.     {  
  15.   
  16.         [OperationContract]  
  17.         List<string> LoginUserDetails(UserDetails userInfo);  
  18.   
  19.   
  20.         // TODO: Add your service operations here  
  21.     }  
  22.   
  23.   
  24.     // Use a data contract as illustrated in the sample below to add composite types to service operations.  
  25.     [DataContract]  
  26.     public class UserDetails  
  27.     {  
  28.         string username = string.Empty;  
  29.         string password = string.Empty;  
  30.         string empid;  
  31.   
  32.         [DataMember]  
  33.         public string UserName  
  34.         {  
  35.             get { return username; }  
  36.             set { username = value; }  
  37.         }  
  38.         [DataMember]  
  39.         public string Password  
  40.         {  
  41.             get { return password; }  
  42.             set { password = value; }  
  43.         }  
  44.         [DataMember]  
  45.         public string RoleId  
  46.         {  
  47.             get { return empid; }  
  48.             set { empid = value; }  
  49.         }  
  50.     }  
  51. }  
Service1.svc
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10.   
  11. namespace WcfServiceLogin  
  12. {  
  13.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.  
  14.     // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.  
  15.     public class Service1 : IService1  
  16.     {   
  17.        public List<string> LoginUserDetails(UserDetails userInfo)  
  18.         {  
  19.             List<string> usr = new List<string>();  
  20.             SqlConnection con = new SqlConnection("Data Source=XXXXXXX;Initial Catalog=XXXXX;User ID=XXXX;Password=XXXXXX");  
  21.             con.Open();  
  22.             SqlCommand cmd = new SqlCommand("select * from tbl_emp where u_name=@UserName and u_pass=@Password", con);  
  23.             cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);  
  24.             cmd.Parameters.AddWithValue("@Password", userInfo.Password);  
  25.   
  26.             SqlDataReader dr = cmd.ExecuteReader();  
  27.   
  28.             if (dr.Read() == true)  
  29.             {  
  30.                 usr.Add(dr[0].ToString());  
  31.                 usr.Add(dr[2].ToString());  
  32.             }  
  33.             con.Close();  
  34.             return usr;  
  35.         }  
  36.     }  
  37. }  
Web.config
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   
  4.   <appSettings>  
  5.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  6.   </appSettings>  
  7.   <connectionStrings>  
  8.     <add name="log" connectionString="Data Source=XXXX; Initial Catalog=XXXX; User Id=XXXX; Password=XXXX" providerName="System.Data.SqlClient"/>  
  9.   </connectionStrings>  
  10.   <system.web>  
  11.     <compilation debug="true" targetFramework="4.5" />  
  12.     <httpRuntime targetFramework="4.5"/>  
  13.   </system.web>  
  14.   <system.serviceModel>  
  15.     <behaviors>  
  16.       <serviceBehaviors>  
  17.         <behavior>  
  18.           <!-- To avoid disclosing metadata information, set the values below to false before deployment -->  
  19.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  20.           <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->  
  21.           <serviceDebug includeExceptionDetailInFaults="false"/>  
  22.         </behavior>  
  23.       </serviceBehaviors>  
  24.     </behaviors>  
  25.     <protocolMapping>  
  26.         <add binding="basicHttpsBinding" scheme="https" />  
  27.     </protocolMapping>      
  28.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  29.   </system.serviceModel>  
  30.   <system.webServer>  
  31.     <modules runAllManagedModulesForAllRequests="true"/>  
  32.     <!--  
  33.         To browse web app root directory during debugging, set the value below to true.  
  34.         Set to false before deployment to avoid disclosing web app folder information.  
  35.       -->  
  36.     <directoryBrowse enabled="true"/>  
  37.   </system.webServer>  
  38.   
  39. </configuration>  
Run service (Press F5).





Input the credentials and click on the Invoke button . If the credentials match, it will revert the emp_id with name.



Now open new Visual Studio window and create a new web application in which we will use the service.







Add Service Reference.



To determine the service, return to the WCF Test Client and copy the Address and paste in an Add Service Reference into the web application.







After clicking Go, we will find that one of our services contains an operation and we can change it here to a suitable name for a namespace.



Add Web Form.





LoginForm.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.aspx.cs" Inherits="WebApplicationLogin.LoginForm" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <table style="font-family:Arial">  
  12.         <tr>  
  13.             <td>  
  14.                 <asp:Label ID="Label1" runat="server" Text="User Name"></asp:Label></td>  
  15.             <td></td>  
  16.             <td>  
  17.                 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>  
  18.         </tr>  
  19.         <tr>  
  20.             <td></td>  
  21.             <td></td>  
  22.             <td></td>  
  23.         </tr>  
  24.          <tr>  
  25.             <td>  
  26.                 <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label></td>  
  27.             <td></td>  
  28.             <td>  
  29.                 <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox></td>  
  30.         </tr>  
  31.         <tr>  
  32.             <td></td>  
  33.             <td></td>  
  34.             <td></td>  
  35.         </tr>  
  36.         <tr>  
  37.             <td>  
  38.                 <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /></td>  
  39.             <td></td>  
  40.             <td>  
  41.                 <asp:Button ID="Button2" runat="server" Text="Reset" OnClick="Button2_Click" /></td>  
  42.         </tr>  
  43.         <tr>  
  44.             <td></td>  
  45.             <td></td>  
  46.             <td></td>  
  47.         </tr>  
  48.         <tr>  
  49.             <td></td>  
  50.             <td colspan="3">  
  51.                 <asp:Label ID="Label3" runat="server"></asp:Label></td>  
  52.             <td></td>  
  53.         </tr>  
  54.     </table>  
  55.     </form>  
  56. </body>  
  57. </html>  
LoginForm.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using WebApplicationLogin.LoginServiceReference;  
  8. namespace WebApplicationLogin  
  9. {  
  10.     public partial class LoginForm : System.Web.UI.Page  
  11.     {  
  12.         LoginServiceReference.Service1Client obj = new LoginServiceReference.Service1Client();  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.   
  16.         }  
  17.   
  18.         protected void Button1_Click(object sender, EventArgs e)  
  19.         {  
  20.             try  
  21.             {  
  22.                 UserDetails userinfo = new UserDetails();  
  23.                 userinfo.UserName = TextBox1.Text;  
  24.                 userinfo.Password = TextBox2.Text;  
  25.                 List<string> msg = obj.LoginUserDetails(userinfo).ToList();  
  26.                 Label3.Text = "Employee Name = " + msg.ElementAt(0)+ " Employee Id = "+ msg.ElementAt(1);  
  27.             }  
  28.             catch (Exception ex)  
  29.             {  
  30.                 Label3.Text = "Wrong Id Or Password";  
  31.             }  
  32.         }  
  33.   
  34.         protected void Button2_Click(object sender, EventArgs e)  
  35.         {  
  36.             TextBox1.Text = "";  
  37.             TextBox2.Text = "";  
  38.         }  
  39.     }  
  40. }  
Now test the web application, but before running the application please be sure the service is running.


Summary

In this article, we made a login service in WCF. We will use that service in our web application to check the login data from SQL Server and if the input data is correct, then the Service will revert the Name and Employee id to the application.


Similar Articles