第一步:新建一个LoginWindow.xaml,设计如下:
代码如下:并为btnLogin按钮添加事件代码如下:
<Window x:Class="HRMSys.UI.LoginWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="LoginWindow" Height="200" Width="300"> <Grid> <TextBlock Height="23" HorizontalAlignment="Left" Margin="20,22,0,0" Name="textBlock1" Text="用户名" VerticalAlignment="Top" /> <TextBlock Height="23" HorizontalAlignment="Left" Margin="20,57,0,0" Name="textBlock2" Text="密码" VerticalAlignment="Top" Width="58" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="87,17,0,0" Name="txtUserName" VerticalAlignment="Top" Width="120" /> <PasswordBox Height="23" HorizontalAlignment="Left" Margin="89,53,0,0" Name="pwbPassword" VerticalAlignment="Top" Width="120" /> <Button Content="登录" Height="23" HorizontalAlignment="Left" Margin="36,102,0,0" Name="btnLogin" VerticalAlignment="Top" Width="75" Click="btnLogin_Click" /> <Button Content="取消" Height="23" HorizontalAlignment="Left" Margin="163,102,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" /> </Grid> </Window>
btnLogin的事件代码:
private void btnLogin_Click(object sender, RoutedEventArgs e) { string username = txtUserName.Text; string pwd = pwbPassword.Password; Operator op=new OperatorDAL().GetByUserName(username); if (op == null) { MessageBox.Show("用户名或者密码错误!"); } else { string dbMD5 = op.Password;//数据库中存储的密码值。 string mymd5 = new MD5().GetMD5(pwd+"love@beijing"); if (dbMD5 == mymd5) { MessageBox.Show("登录成功!"); } else { MessageBox.Show("用户名或者密码错误!"); } } }
再次在MainWindow中添加Loaded事件代码如下:
private void Window_Loaded(object sender, RoutedEventArgs e) { LoginWindow win = new LoginWindow(); win.ShowDialog(); }
修改下 miOperatorMgr_Click(object sender, RoutedEventArgs e)如下代码:
private void miOperatorMgr_Click(object sender, RoutedEventArgs e) { //写一个固定用户进行测试 string str = "123"; string md5 = new MD5().GetMD5(str+"love@beijing"); Operator op = new Operator();//注意1.App.config要放在UI层中。 op.UserName = "王五";//2.DAL层通过ConfigurationManager可以读取主项目中的配置文件中得信息。 op.Password = md5;//引用关系:DAL引用Model,UI引用DAL和Model OperatorDAL dal = new OperatorDAL(); dal.Insert(op); }