NEWS

[C#] Hướng dẫn sử dụng hộp thoại chứng thực Windows Credential trong lập trình Winform

[C#] Hướng dẫn sử dụng hộp thoại chứng thực Windows Credential trong lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 4817 10:56:37, 13/07/2020DEVEXPRESS   In bài viết

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.

WindowsCredential_chrome

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:

WindowsCredential_demo_csharp

Ở 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!

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn sử dụng hộp thoại chứng thực Windows Credential trong lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 4817 10:56:37, 13/07/2020DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.