Đăng nhập với Google Account API

Đăng nhập với Google Account API

Trước tiên bạn cần hiểu về API là gì ?

Để đăng nhập với Google Account (OAuth 2.0) trong ASPX C# (ASP.NET Web Forms), bạn cần tích hợp Google Sign-In qua các bước sau:

1. Tổng quan các bước

Tạo Project trên Google Cloud Console

Lấy Client ID và Client Secret

Gắn nút Google Sign-In vào Default.aspx

Viết mã xử lý xác thực trong Default.aspx.cs

    (Tuỳ chọn) Đọc thông tin người dùng từ token

Bước 1: Tạo Project và lấy Client ID

    Truy cập: https://console.cloud.google.com/
    Tạo Project mới
    Vào APIs & Services > Credentials
    Chọn Create Credentials > OAuth 2.0 Client IDs

    Chọn:

        Application type: Web application

        Authorized redirect URI: https://localhost:44300/Default.aspx (tuỳ cổng bạn chạy)

➡️ Lưu lại:
    Client ID
    Client Secret

Bước 2: Gắn nút Google Sign-In vào ASPX

Trong Default.aspx:

<head runat="server">
  <script src="https://accounts.google.com/gsi/client" async defer></script>
</head>
<body>
  <form id="form1" runat="server">
    <div id="g_id_onload"
         data-client_id="YOUR_CLIENT_ID"
         data-login_uri="https://localhost:44300/Default.aspx"
         data-auto_prompt="false">
    </div>

    <div class="g_id_signin"
         data-type="standard"
         data-size="large"
         data-theme="outline"
         data-text="sign_in_with"
         data-shape="rectangular"
         data-logo_alignment="left">
    </div>
  </form>
</body>

📌 Thay YOUR_CLIENT_ID bằng Client ID bạn nhận được.

Bước 3: Xử lý token ở server (Default.aspx.cs)

Khi người dùng đăng nhập, Google sẽ POST credential (mã JWT) đến Default.aspx. Ta sẽ xác thực nó:

using System;
using Google.Apis.Auth;
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack && Request.Form["credential"] != null)
    {
        var token = Request.Form["credential"];
        try
        {
            var payload = GoogleJsonWebSignature.ValidateAsync(token).Result;

            // Lấy thông tin người dùng
            string email = payload.Email;
            string name = payload.Name;
            string picture = payload.Picture;

            // Lưu thông tin người dùng vào Session hoặc Database
            Session["email"] = email;
            Session["name"] = name;

            Response.Write("Xin chào: " + name + " (" + email + ")");
        }
        catch (Exception ex)
        {
            Response.Write("Lỗi xác thực: " + ex.Message);
        }
    }
}

Bước 4: Cài thư viện Google API

Mở NuGet Package Manager Console, gõ:

Install-Package Google.Apis.Auth

✅ Kết quả

Sau khi người dùng nhấn Google Sign-In, Google sẽ gửi JWT về Default.aspx. Server xác thực nó và lấy thông tin người dùng.

🔒 Lưu ý bảo mật

    Sử dụng HTTPS để bảo mật credential
    Nên cấu hình Authorized redirect URI đúng trên Google Cloud Console
    JWT chỉ nên xác thực bằng Google API chính thức

Tài liệu:  https://developers.google.com/?hl=vi

 

Đăng nhập với Google Account API