Credential Manager
trên Windows.[CSHARP] Hướng dẫn lưu trữ mật khẩu ở Credential Manage Windows
Trên Microsoft Windows, có một ứng dụng Credential Manager dùng để lưu trữ thông tin username và password của một ứng dụng hoặc Website.Để truy cập vào
Credential Manager
:Các bạn vào
ControlPanel
> All Control Panel Items
> Credential manger
như hình bên dưới.Bài viết này mình sẽ hướng dẫn các bạn cách lưu trữ mật khẩu ứng dụng vào đây, như hình bên dưới: Ở hình trên, các bạn thấy mình đã lưu trữ ứng dụng:
laptrinhvbApp
Để thực hiện, các bạn cần cài đặt thư viện Credentials
từ Nuget
:
PM> NuGet\Install-Package Credentials -Version 2.0.0.30
Bước 1: Tạo một class PasswordRepository chứa hai phương thức: SavePassword
và GetPassword
public class PasswordRepository { public void SavePassword(string AppName, string password) { using (var cred = new Credential()) { cred.Password = password; cred.Target = AppName; cred.Type = CredentialType.Generic; cred.PersistanceType = PersistanceType.LocalComputer; cred.Save(); } } public string GetPassword(string AppName) { using (var cred = new Credential()) { cred.Target = AppName; cred.Load(); return cred.Password; } } }
Bước 2: Sử dụng hàm trên để lưu trữ mật khẩu
private void button1_Click(object sender, EventArgs e) { var AppName = "laptrinhvbApp"; new PasswordRepository().SavePassword(AppName, "Laptrinhvb.net"); var data = new PasswordRepository().GetPassword(AppName); }Chúc các bạn thành công với thủ thuật lưu mật khẩu trên, và không lo việc quên mật khẩu nữa.
Theo LaptrinhVB.Net