zoukankan      html  css  js  c++  java
  • C# Protect the Password inside a TextBox ZZ

    If the Text property is called, it will send an WM_GETTEXT message, so it will surely be an internal (safe) call. But if that message is received and the Text property wasn't called, then it might be risky to return the password, so we'll not process that message.

    I wrote a "safer" TextBox here, just to show you the idea, feel free to write your own or simply improve this one.

    class ProtectedTextBox : TextBox
    {
        // the malicious message, that needs to be handled
        private const int WM_GETTEXT = 0x000D;
    
        // 'true' if the messages are sent from our program (from Text property)
        // 'false' if they're sent by anything else 
        bool allowAccess { get; set; }
    
        public override string Text   // overriding Text property
        {
            get
            {
                allowAccess = true;    // allow WM_GETTEXT (because it's an internal call)
                return base.Text;  //this sends the message above in order to retrieve the TextBox's value
            }
            set
            {
                base.Text = value;
            }
        }
    
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_GETTEXT)  // if the message is WM_GETTEXT 
            { 
                if (allowAccess)  // and it comes from the Text property
                {
                    allowAccess = false;   //we temporarily remove the access
                    base.WndProc(ref m);  //and finally, process the message
                }
            }
            else
                base.WndProc(ref m);
        }
    }
    View Code

  • 相关阅读:
    架构中那些需要注意的事儿
    谈谈测试环境管理与实践
    响应式布局
    flex布局
    crontab定时任务
    SpringMVC Json自定义序列化和反序列化
    Tensorflow 使用TPU训练
    使用Selenium从IEEE与谷歌学术批量爬取BibTex文献引用
    Pyecharts——Python高级可视化
    Python图像处理库——PIL
  • 原文地址:https://www.cnblogs.com/zeroone/p/3378760.html
Copyright © 2011-2022 走看看