NEWS

[C#] Hiển thị cửa sổ user consent trên winform NET Core 6

[C#] Hiển thị cửa sổ user consent trên winform NET Core 6
Đăng bởi: Thảo Meo - Lượt xem: 1779 07:57:05, 16/09/2022DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách gọi cửa sổ User Consent trên Winform Net Core 6 lập trình C#.

[C#] Getting user consent in Winform NET Core

Khi các bạn viết ứng dụng, đến đoạn xử lý một sự kiện nhạy cảm cần đòi hỏi tính xác thực của người dùng.

Trên NET 6 có cung cấp cho các bạn lớp class Windows.Security.Credentials để các bạn có thể gọi cửa sổ chứng thực tài khoản windows, nếu chứng thực đúng mới thực hiện hành động.

Ví dụ ứng dụng demo mình dưới đây:

user_consent_csharp

Các bước thực hiện ứng dụng.

Bước 1: Các bạn chỉnh sửa file .csproj thêm vào đoạn TargetFramework như code bên dưới

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
  </PropertyGroup>
</Project>

Bước 2: Viết một phương thức ConsentAsync để hiển thị cửa số User Consent

public async Task<bool> ConsentAsync(string reason)
{
    var availability = await Windows.Security.Credentials.UI.UserConsentVerifier.CheckAvailabilityAsync();
    if (availability == Windows.Security.Credentials.UI.UserConsentVerifierAvailability.Available)
    {
        var result = await Windows.Security.Credentials.UI.UserConsentVerifier.RequestVerificationAsync(reason);
        return result == Windows.Security.Credentials.UI.UserConsentVerificationResult.Verified;
    }

    return false;
}

Bước 2: Xứ lý sự kiện khi click chuột vào nút Payment

private async void btnPayment_Click(object sender, EventArgs e)
{
    if (await ConsentAsync("Bạn cần chứng thực tài khoản trước khi mua hàng?"))
    {
       lblResult.Text = "Chứng thực thành công, bạn ơi";
    }
    else
    {
        lblResult.Text = "Chứng thực thất bại";
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hiển thị cửa sổ user consent trên winform NET Core 6
Đăng bởi: Thảo Meo - Lượt xem: 1779 07:57:05, 16/09/2022DEVEXPRESS   In bài viết

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

Đọc tiếp
.