- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
[C#] Hướng dẫn sử dụng hộp thoại chứng thực Windows Credential trong lập trình Winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn gọi hộp thoại chứng thực tài khoản Windows Credential trong lập trình C# Winform.
[C#] Show Windows Credential Dialog Winform
Trong một số chức năng của các ứng dụng, các bạn có thể thấy khi muốn gọi phần mềm truy cập vào những chức năng cá nhân.
Và phần mềm muốn xác định là người đang sử dụng có đúng là tài khoản của máy tính của mình không, thì ứng dụng đó thường hiển thị Windows Credential để chứng thực tài khoản một lần nữa.
Thông thường các bạn dễ nhìn thấy là phần mềm trình duyệt Web Google Chrome khi các bạn vào phần quản lý mật khẩu, và muốn hiển thị mật khẩu thì Chrome bắt buộc người sử dụng phải xác thực lại thông tin đăng nhập tài khoản Windows.
Khi nhập vào đúng thông tin thì Google Chrome mới hiện thị thông tin các mật khẩu đã lưu của các trang web của bạn xuất hiện.
Dưới đây là giao diện demo ứng dụng Windows Credential C# Winform:
Ở ví dụ hình ảnh demo trên các bạn sẽ thấy, mình có demo hai giao diện chứng thực tài khoản Windows.
Khi đăng nhập xong sẽ trả về 3 thông tin bao gồm: Username, Password và Trạng thái login.
Source code C# Windows Credential:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsCredential
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool IsLoginSuccess = false;
StringBuilder userPassword = new StringBuilder(), userID = new StringBuilder(@"HOABINHCO\NGUYENTHAO");
var credUI = new CredentialHelper.CREDUI_INFO();
credUI.cbSize = Marshal.SizeOf(credUI);
credUI.pszCaptionText = "https://laptrinhvb.net";
credUI.pszMessageText = "Nhập tài khoản windows chứng thực";
credUI.hwndParent = this.Handle;
bool save = false;
var flags = CredentialHelper.CREDUI_FLAGS.ALWAYS_SHOW_UI | CredentialHelper.CREDUI_FLAGS.GENERIC_CREDENTIALS;
var returnCode = CredentialHelper.CredUIReturnCodes.NO_ERROR;
bool validCredentials = false;
do
{
returnCode = CredentialHelper.CredUIPromptForCredentials(ref credUI, "Laptrinhvb.net", IntPtr.Zero, 0, userID, 100, userPassword, 100, ref save, flags);
if (returnCode == CredentialHelper.CredUIReturnCodes.NO_ERROR)
{
validCredentials = CredentialValidator.ValidateCredential(userID.ToString(), userPassword.ToString());
}
flags |= CredentialHelper.CREDUI_FLAGS.INCORRECT_PASSWORD;
}
while (returnCode == CredentialHelper.CredUIReturnCodes.NO_ERROR && !validCredentials);
lbl_username1.Text = userID.ToString();
lbl_password1.Text = userPassword.ToString();
IsLoginSuccess = (returnCode == CredentialHelper.CredUIReturnCodes.NO_ERROR && validCredentials);
if (IsLoginSuccess)
{
lbl_status1.Text = "Login Success.";
}
else
{
lbl_status1.Text = "Login Fails.";
}
}
private void btn_openCredential2_Click(object sender, EventArgs e)
{
bool IsLoginSuccess = false;
StringBuilder userPassword = new StringBuilder(), userID = new StringBuilder(@"HOABINHCO\NGUYENTHAO");
var credUI = new CredentialHelper.CREDUI_INFO();
credUI.cbSize = Marshal.SizeOf(credUI);
credUI.pszCaptionText = "https://laptrinhvb.net";
credUI.pszMessageText = "Nhập tài khoản windows chứng thực";
credUI.hwndParent = this.Handle;
bool save = false;
var returnCode = CredentialHelper.CredUIReturnCodes.NO_ERROR;
bool validCredentials = false;
int errorcode = 0;
uint dialogReturn;
uint authPackage = 0;
IntPtr outCredBuffer;
uint outCredSize;
var flags = CredentialHelper.CredUIWinFlags.Generic;
string UserName = "", Password="";
do
{
var result = CredentialHelper.CredUIPromptForWindowsCredentials(ref credUI,
0,
ref authPackage,
IntPtr.Zero,
0,
out outCredBuffer,
out outCredSize,
ref save,
flags);
if (result == CredentialHelper.CredUIReturnCodes.NO_ERROR)
{
StringBuilder userName = new StringBuilder(CredentialHelper.CREDUI_MAX_USERNAME_LENGTH);
StringBuilder password2 = new StringBuilder(CredentialHelper.CREDUI_MAX_PASSWORD_LENGTH);
uint userNameSize = (uint)userName.Capacity;
uint passwordSize = (uint)password2.Capacity;
uint domainSize = 0;
if (!CredentialHelper.CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, userName, ref userNameSize, null, ref domainSize, password2, ref passwordSize))
return;
UserName = userName.ToString();
Password = password2.ToString();
validCredentials = CredentialValidator.ValidateCredential(UserName, Password);
}
if (result != 0) break;
}
while (returnCode == CredentialHelper.CredUIReturnCodes.NO_ERROR && !validCredentials);
lbl_username2.Text = UserName.ToString();
lbl_password2.Text = Password.ToString();
IsLoginSuccess = (returnCode == CredentialHelper.CredUIReturnCodes.NO_ERROR && validCredentials);
if (IsLoginSuccess)
{
lbl_status2.Text = "Login Success.";
}
else
{
lbl_status2.Text = "Login Fails.";
}
}
}
}
Thanks for watching!