Tuesday, June 7, 2011

Login Authentication in WPF using WebServices



===========================================================================
Xaml Page
===========================================================================
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Width="500" Height="500" MaxHeight="500" MaxWidth="500" MinHeight="500" MinWidth="500" WindowStartupLocation="CenterScreen">
<--Button Name="btn" Margin="152,242,0,196" Content="Submit" Click="btn_Click_Submit" Foreground="White" HorizontalAlignment="Left" Width="72" >

<--Label Height="28" Margin="130,121,0,0" Name="label1" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Canvas.Left="113" Canvas.Top="56">User name
<--Label Margin="131,178,0,0" Name="label2" Foreground="White" HorizontalAlignment="Left" Width="70" Height="23" VerticalAlignment="Top" Canvas.Left="113" Canvas.Top="75">Password
<--TextBox Height="26" Margin="0,124,120,0" MaxWidth="150" MinWidth="150" HorizontalAlignment="Right" Name="textBox1" VerticalAlignment="Top" Canvas.Left="134" Canvas.Top="53" Width="150" /-->
<--PasswordBox Margin="0,177,119,0" Name="textBox2" MinWidth="150" MaxWidth="150" HorizontalAlignment="Right" Height="24" VerticalAlignment="Top" Canvas.Left="134" Canvas.Top="78" Width="150" /-->
<--Button Foreground="White" Click="btnClick_Cancel" HorizontalAlignment="Right" Margin="0,241,145,196" Name="btn1" Width="79" Canvas.Left="276" Canvas.Top="90"-->Cancel
<--Label Foreground="White" Height="28" Name="label3" Margin="177,320,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="138" /-->
===========================================================================
Creating a WebService
===========================================================================
1.open a new project and click the asp.net web service template to create a new web service.
2.create a separate method to authenticate username and password as follows.

[WebMethod]
public int ValidateUsers(string UserName,string Password)
{
int count;
string connection = ("Data Source=Dotnet;Initial Catalog=shiva;User Id=sa;Password=2005");
SqlConnection conn = new SqlConnection(connection);
conn.Open();
SqlCommand comm = new SqlCommand("[Login_Authentication]", conn);
SqlParameter para = new SqlParameter("@UserName", UserName);
comm.Parameters.Add(para);
SqlParameter para1 = new SqlParameter("Password", Password);
comm.Parameters.Add(para1);
comm.CommandType = CommandType.StoredProcedure;
count = (int)comm.ExecuteScalar();

return count;
conn.Close();
}

give your credentials according to your database.


=========================================================================
Code Behind
=========================================================================
1.Run the asp.net service that u created.
2.copy the url that u can see in the address bar.
3.
right click your project and click add the service reference and paste the copied url into it and give a name for the added reference and click finish.
4.now the service has been added to your client application i.e to your wpf application.any changes that you make in your service later will be eefective only if you update your service in your wpf service reference.


private void btn_Click_Submit(object sender, RoutedEventArgs e)
{
Service1SoapClient client = new Service1SoapClient();

var value = client.ValidateUsers(textBox1.Text,textBox2.Password);

if (value > 0)
{
label3.Content = "Login Successfull!";
ListBox LB = new ListBox();
LB.Show();
this.Close();
}
else
{
label3.Content = "Login Failed";
}
}


private void btnClick_Cancel(object sender, RoutedEventArgs e)
{
this.Close();
}


Service1SoapClient is the client of the added service reference. each asp.net service has a client through which we can access its methods. so here ServiceSoapClient is the Client of the ServiceReference.


===========================================================================
Stored Procedure
===========================================================================
USE [shiva]
GO
/****** Object: StoredProcedure [dbo].[Login_Authentication] Script Date: 06/07/2011 12:28:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[Login_Authentication]
(@UserName varchar(255),@Password varchar(255))
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select COUNT (*) from Login where UserName=@UserName and Password=@Password
END


1.Login_Authentication is a stored procedure which queries the table using count Query to return a value of 1 if the given condition is satisfied.

2.Here the condition is ito check if the given user name and password matches the record in the table Login.if it matches then it returns the value of 1,else it returns 0.

3.so if the give username and password matches then the ValidateUsers method returns 1 ,else 0.

4.so if the "Value" in buttonclick is greater than 0 then it will show login is successfull! else login is not successfull!!




I hope the time you spent on this blog is really usefull!
Thanks!!!


No comments:

Post a Comment