How to use Sliverlight enabled WCF service for login functionality?


As we can not use ADO.net directly in Sliverlight5. We can use it through sliverlight enabled WCF services. I hope this article will be useful for you all. Silverlight5 gives rich look and feel to your application.

How to use Sliverlight enabled WCF service for login functionality?
Assumption :
Consider we have login table in SQL server 2008 and we want to check whether valid user has logged in the system or not then we can use following piece of code.
How to add svc file in web project?
In silverlight 5 application, right click on web project and add "New Item" as "Silverlight-enabled WCF service". It will create Service1.svc by default.
Approach:
In login.xaml page , consider one button for login and two textboxes are present for user id and password. You have to send input values of login id and password to this Service1.svc so that it will check if these values are present in the database or not and return results accordingly.
Description: In Login.xaml.cs page write following code which is used to create object of WCF service. LoginFunctionality is a public class present in the service.
We can not use SQL queries directly in the silverlight 5 to retrieve data from database so, what we will do is, we will create one WCF service which is silverlight enabled and fetch data through it.


//Declare string as null
string str = null;
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
LFunctionality.LoginFunctionalityClient lfc = new LFunctionality.LoginFunctionalityClient();
lfc.ChkCredentialsCompleted += new EventHandler<LFunctionality.ChkCredentialsCompletedEventArgs>(ChkCredentialsCompleted);
str = txtLogin.Text;
//login and password entered by user will be sent to service.
lfc.ChkCredentialsAsync(txtLogin.Text , txtPwd.Password);
}

void ChkCredentialsCompleted(object sender, LFunctionality.ChkCredentialsCompletedEventArgs e)
{
ListBox lst = new ListBox();
//e.Result will contain data retrived from the database.
lst.ItemsSource = e.Result;

if (lst.Items.Count == 1)
{
//This will redirect to the next page for valid user. The value of str will be sent in the constructor of nextpage because if user needs to display Welcome username then that message can be displayed with the help of str value.
this.Content = new NextPage(str);
}
}

Creation of class file :
Create class file clsLoginFields.cs, so that we can add DataContract and Datamember in it.
USERNAME and PASSWORD are properties defined in the clsLoginFields.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace your_prj_namespace.Web
{
[DataContract]
public class clsLoginFields
{
[DataMember]
public string USERNAME { get; set; }
[DataMember]
public string PASSWORD { get; set; }

}
}

Following code has to be written in .svc file. i.e. WCF service file.

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace your_prj_namespace.Web
{
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class LoginFunctionality
{
static string connectionString = "Data Source=your_server_name\\SQLserver_instance_name;Initial Catalog=your_databasename;Integrated Security=True;";
[OperationContract]

public List<clsLoginFields> ChkCredentials(string _LOGINID, string _PWD)
{
SqlConnection sqlConnection = new SqlConnection(connectionString);
DataSet objSet = new DataSet();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "Select * FROM logintablename WHERE USERNAME like '" + _LOGINID + "'" + " AND PASSWORD like '" + _PWD + "'" ;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(objSet);
List<clsLoginFields> outputText = new List<clsLoginFields>();
clsLoginFields obj_login_data;
if (objSet.Tables.Count > 0)
{

foreach (DataRow dr in objSet.Tables[0].Rows)
{
obj_login_data= new clsLoginFields();
obj_login_data.USERNAME = dr["USERNAME"].ToString();
obj_login_data.PASSWORD = dr["PASSWORD"].ToString();
outputText.Add(obj_login_data);
}
}

return outputText;

}
}
}


Comments

Author: Phagu Mahato05 Jan 2014 Member Level: Gold   Points : 8

Silverlight applications frequently need to access some data or functionality on the back-end server. For example, if you are writing a Student management application, the server might contain a database of Students that your Silverlight application will need to retrieve.To add a Silverlight-enabled WCF service to an existing Web site or Web application

From the File menu, select New, then Project, then Silverlight from the Project types, and then select Silverlight Application from the Visual Studio installed templates. Accept the default Name SilverlightApplicaton1 and click OK.
n the New Silverlight Application wizard that pops up, accept the Host the Silverlight application in a new Web site default that is selected. Accept the default value SilverlightApplication1.Web for the New Web project name and the default ASP.NET Web Application Project

 using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace SilverlightApplication1.Web
{
[ServiceContract(Namespace = "")]

public class StudentService
{
[OperationContract]
public int CountUsers()
{
return 2;
}
[OperationContract]
public User GetUser(int id)
{
if (id == 1)
{
return new User() { IsMember = true, Name = "Prakash", Age = 24 };
}
else
{
return new User() { IsMember = false, Name = "Tonny John", Age = 64 };
}
}
}

public class User
{

public bool IsMember { get; set; }


public string Name { get; set; }


public int Age { get; set; }
}

}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: