- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[C#] Tạo SecureString TextBox trong winform
Xin chào các bạn bài viết hôm nay mình sẽ hướng dẫn các bạn sử dụng lớp Namespace Secure String để tạo một component SecureString TextBox trên winform bằng ngôn ngữ C#.
[C#] Tạo mới control SecureString TextBox trong winform
Vậy SecureString Textbox là một control mình tạo ra để tránh hạn chế khả năng người dùng xem mật khẩu trong ô TextBox.
Mặc định, khi các bạn lập trình ứng dụng, thì các bạn thường tạo ra một Form Login, để nhập username và password.
Ở ô password, chúng ta thường sử dụng TextBox
để nhập liệu và set thuộc tính (property) để cho TextBox đó không hiển thị nội dung.
Tuy nhiên, các bạn có thể sử dụng công cụ Spy++, hoặc AutoIT Windows Info để xem mật khẩu một cách dễ dàng.
Dưới đây, là demo mình xem mật khẩu của ô TextBox bình thường của ứng dụng Winform:
Ở hình ảnh trên các bạn thấy mình dùng ứng dụng Spy++ vào là có thể xem được mật khẩu qua ô Caption.
Và dưới đây là ứng dụng SecureString TextBox, mình cũng dùng Spy++ để thử:
Bây giờ mình sẽ bắt đầu tạo Component SSTextBox
, các bạn tạo cho mình một UserControl với tên SSTextBoxt.cs
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
namespace SecureStringTextBox
{
public partial class SSTextBox : UserControl
{
char _passwordChar = '●';
SecureString _secureString = new SecureString();
public SecureString SecureString
{
get { return _secureString; }
}
public char PasswordChar
{
get { return _passwordChar; }
set { _passwordChar = value; }
}
public SSTextBox()
{
InitializeComponent();
}
private void InputBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\b')
ProcessBackspace();
else
ProcessNewCharacter(e.KeyChar);
e.Handled = true;
}
private void ProcessNewCharacter(char character)
{
if (InputBox.SelectionLength > 0)
{
RemoveSelectedCharacters();
}
_secureString.InsertAt(InputBox.SelectionStart, character);
ResetDisplayCharacters(InputBox.SelectionStart + 1);
}
private void RemoveSelectedCharacters()
{
for (int i = 0; i < InputBox.SelectionLength; i++)
{
_secureString.RemoveAt(InputBox.SelectionStart);
}
}
private void ResetDisplayCharacters(int caretPosition)
{
InputBox.Text = new string(_passwordChar, _secureString.Length);
InputBox.SelectionStart = caretPosition;
}
private void ProcessBackspace()
{
if (InputBox.SelectionLength > 0)
{
RemoveSelectedCharacters();
ResetDisplayCharacters(InputBox.SelectionStart);
}
else if (InputBox.SelectionStart > 0)
{
_secureString.RemoveAt(InputBox.SelectionStart - 1);
ResetDisplayCharacters(InputBox.SelectionStart - 1);
}
}
private void InputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
ProcessDelete();
e.Handled = true;
}
else if (IsIgnorableKey(e.KeyCode))
{
e.Handled = true;
}
}
private bool IsIgnorableKey(Keys key)
{
return key == Keys.Escape || key == Keys.Enter;
}
private void ProcessDelete()
{
if (InputBox.SelectionLength > 0)
{
RemoveSelectedCharacters();
}
else if (InputBox.SelectionStart < InputBox.Text.Length)
{
_secureString.RemoveAt(InputBox.SelectionStart);
}
ResetDisplayCharacters(InputBox.SelectionStart);
}
}
}
Bắt đầu, sử dụng mình sẽ đọc mật khẩu ở Form1.cs
C#:
Chúng ta sẽ sử dụng hàm ExtractStringFromSecureString()
để lấy kiểu String từ SecureString.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace SecureStringTextBox
{
public partial class DemoForm : Form
{
public DemoForm()
{
InitializeComponent();
}
private void ShowStringButton_Click(object sender, EventArgs e)
{
IntPtr bstrPointer = Marshal.SecureStringToBSTR(SecureInput.SecureString);
string noLongerSecure = ExtractStringFromSecureString(bstrPointer);
MessageBox.Show(noLongerSecure);
}
private static string ExtractStringFromSecureString(IntPtr bstrPointer)
{
string noLongerSecure = "";
for (int i = 0; ; i += 2)
{
byte lo = Marshal.ReadByte(bstrPointer, i);
byte hi = Marshal.ReadByte(bstrPointer, i + 1);
long l = lo + hi * 256;
if (lo != 0)
noLongerSecure += (char)l;
else
break;
}
Marshal.ZeroFreeBSTR(bstrPointer);
return noLongerSecure;
}
}
}
Thanks for watching!