Witajcie. Mam problem z wywołaniem funkcji z WCF. Kompletnie nie wiem jak się do tego zabrać. Mam stworzony interfejs z miejscem na login i hasło oraz button zaloguj. Kod strony logowania Windows Phone wygląda tak:
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private string _token;
Service1Client client = new Service1Client();
private void Button_Click(object sender, RoutedEventArgs e)
{
client.LogowanieCompleted+=client_LogowanieCompleted;
client.LogowanieAsync(tb1.Text,tb2.Text);
}
void client_LogowanieCompleted(object sender, LogowanieCompletedEventArgs e)
{
label.Text = e.Result;
// _token = e.Result;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Dodaj.xaml", UriKind.Relative));
}
private void JestemUzytkownikiem_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/BlankPage1.xaml", UriKind.Relative));
}
}
}
<phone:PhoneApplicationPage
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"/>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="14,0,10,0">
<Image x:Name="obraz" HorizontalAlignment="Left" Height="763" Margin="-13,-40,-14,0" VerticalAlignment="Top" Width="483" Stretch="Fill" Source="Assets/tlo.jpg"/>
<TextBlock x:Name="label" HorizontalAlignment="Left" Margin="63,305,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
<TextBox x:Name="tb1" HorizontalAlignment="Left" Height="72" Margin="0,31,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="456"/>
<TextBox x:Name="tb2" HorizontalAlignment="Left" Height="72" Margin="0,90,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="456"/>
<Button x:Name="Zaloguj" Content="Zaloguj" HorizontalAlignment="Right" Margin="328,535,0,0" VerticalAlignment="Bottom" Click="Button_Click_1"/>
<Button x:Name="JestemUzytkownikiem" Content="Jestem Użytkownikem" HorizontalAlignment="Left" Margin="0,535,0,0" VerticalAlignment="Bottom" Click="JestemUzytkownikiem_Click"/>
</Grid>
<!--Uncomment to see an alignment grid to help ensure your controls are
aligned on common boundaries. The image has a top margin of -32px to
account for the System Tray. Set this to 0 (or remove the margin altogether)
if the System Tray is hidden.
Before shipping remove this XAML and the image itself.-->
<!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>
</phone:PhoneApplicationPage>
Natomiast kod samego logowania w WCF wygląda tak:
namespace WcfService1
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
string token;
string aa;
//public static string genMD5(string input)
//{
// System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
// byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
// byte[] hashBytes = md5.ComputeHash(inputBytes);
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("X2")); } return sb.ToString().ToLower();
//}
public string Logowanie(string login, string haslo)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(haslo);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("X2")); } haslo= sb.ToString().ToLower();
//SPRAWDZ CZY ISTNIEJE TAKI LOGIN I HASLO JESLI TAK TO:
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server=.;Database=projekt1;Trusted_Connection=True;";
//
String query = "select * from admini where nick = '" + login + "'and haslo = '" + haslo + "'";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dbr;
con.Open();
dbr = cmd.ExecuteReader();
int count = 0;
while (dbr.Read())
{
count = count + 1;
}
if (count == 1)
{
token = Encrypt(GenerujToken(login, haslo), "haslo", "password");
return SprawdźSesje(token);
}
else if (count < 1)
{
aa = "niezalogowany";
}
return aa;
}
private string GenerujToken(string login, string haslo)
{
return login + ";" + DateTime.Now.AddMinutes(10) + ";" + haslo;
}
public string SprawdźSesje(string token)
{
token = Decrypt(token, "haslo", "password");
var listaztokenu = token.Split(Convert.ToChar(";")).ToList();
if (Convert.ToDateTime(listaztokenu[1]) > DateTime.Now)
{
return "zalogowany";
}
else
{
return "NIE zalogowany";
}
// return token;
}
I nie wiem jak to ma wyglądać w kodzie samej aplikacji żeby logowanie działało.