• MD5加盐实现登录


    第一步:新建一个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);
    
            }
     
  • 相关阅读:
    面试只要问到分布式,必问分布式锁
    Java编程中忽略这些细节,Bug肯定找上你
    不止承上启下,带你了解工业物联网关
    论文解读二十七:文本行识别模型的再思考
    并发高?可能是编译优化引发有序性问题
    论文解读丨LayoutLM: 面向文档理解的文本与版面预训练
    SQL优化老出错,那是你没弄明白MySQL解释计划
    SQL反模式学习笔记1 开篇
    SQL Server中自定义函数:用指定的分隔符号分割字符串
    .NET软件开发与常用工具清单
  • 原文地址:https://www.cnblogs.com/shixunle/p/3331043.html
Copyright © 2020-2023  润新知