×
Namespaces

Variants
Actions

Windows Phone应用程序的登录对话框

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon UI.png
SignpostIcon WP7 70px.png
Article Metadata

代码示例
兼容于
文章
翻译:
Hazel Xie
最后由 hamishwillee 在 15 Jul 2013 编辑

某些应用程序可能需要访问控制,因为它们存储了敏感数据。本文展示了如何创建一个登陆对话框来保护该应用程序/数据的访问

介绍

实现以一个登录对话框作为页面在Windows Phone里暴露了一个问题:让导航栈按序和以导航实现存储需求是有一点困难的。这部分原因是在错误密码情况下没有退出方法。从页面栈(使用NavigationService.RemoveBackEntry, 该方法添加进了芒果系统)中移除登录页是必要的,那样当按下返回键应用程序不要再经过登录页面而结束

解决方案

一个更简单的方法是使用动态在主页顶端创建一个弹窗

如下所述,该弹窗是在该页面的Loaded handler中创建

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (!app.IsAuthenticated)
{
p = new Popup();
LayoutRoot.Children.Add(p);
ApplicationBar.IsVisible = false;
 
// Set where the popup will show up on the screen.
p.VerticalOffset = 60;
p.HorizontalOffset = 25;
 
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.White);
border.BorderThickness = new Thickness(5.0);
 
StackPanel panel1 = new StackPanel();
panel1.Background = new SolidColorBrush(Colors.Black);
panel1.Width = LayoutRoot.ActualWidth - 2 * p.HorizontalOffset;
 
Button button1 = new Button();
button1.Content = "OK";
button1.Margin = new Thickness(5.0);
button1.Click += new RoutedEventHandler(loginbutton_Click);
TextBlock textblock1 = new TextBlock();
textblock1.TextWrapping = TextWrapping.Wrap;
if (app.FirstRun)
textblock1.Text = "Please enter your choice of a password:";
else
textblock1.Text = "Please enter your password";
textblock1.Margin = new Thickness(5.0);
textblock1.FontSize = 30;
textblock1.Foreground = new SolidColorBrush(Colors.White);
pb = new PasswordBox();
pb.KeyDown += new KeyEventHandler(pb_KeyDown);
panel1.Children.Add(textblock1);
panel1.Children.Add(pb);
panel1.Children.Add(button1);
border.Child = panel1;
 
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
 
// Open the popup.
p.IsOpen = true;
pb.Focus();
}
else
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
}

密码的评测在登录按钮处理事件中进行的,如下所示。

private void loginbutton_Click(object sender, RoutedEventArgs e)
{
// Close the popup.
p.IsOpen = false;
if (app.FirstRun)
{
app.PwdHash = CryptoUtil.GetHashCode(pb.Password);
app.IsAuthenticated = true;
app.FirstRun = false;
App.ViewModel.LoadData();
}
else
app.IsAuthenticated = CryptoUtil.GetHashCode(pb.Password).CompareTo(app.PwdHash) == 0;
 
if (app.IsAuthenticated)
{
DataContext = App.ViewModel;
ApplicationBar.IsVisible = true;
}
else
{
tb = new TextBlock();
tb.Text = "Wrong password.";
tb.FontSize = 36;
tb.Foreground = new SolidColorBrush(Colors.Red);
tb.Margin = new Thickness(12);
tb.SetValue(Grid.RowProperty, 2);
LayoutRoot.Children.Add(tb);
ApplicationBar.IsVisible = true;
(ApplicationBar.MenuItems[0] as ApplicationBarMenuItem).IsEnabled = false;
 
DataContext = null;
}
}

在密码框中按下Enter键模拟单击了登录按钮

void pb_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
loginbutton_Click(this, new RoutedEventArgs());
}

当返回键按下,弹窗打开,密码检查必须执行。

private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (p.IsOpen)
{
loginbutton_Click(this, new RoutedEventArgs());
e.Cancel = true;
}
}

在第一次运行该程序用户需要提供密码。密码本身不会保存在程序中,只存储哈希代码。在成功情况下,输入的密码的哈希用于计算然后与存储的哈希代码进行比较。请看How to encrypt your application data in Windows Phone

示例代码

登录对话框的代码已经添加到How to encrypt your application data in Windows Phone文章中示例Media:EncryptionSample.zip

This page was last modified on 15 July 2013, at 14:29.
121 page views in the last 30 days.