[研究]ASP.NET, WebForm, 試用 Google reCAPTCHA v3 人機驗證
2024-03-23
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
********************************************************************************
第一次嘗試使用,要自己去註冊帳號,設定哪個網站或 IP 使用,設定使用 v2 或 v3 版。
reCAPTCHA v3
https://developers.google.com/recaptcha/docs/v3?hl=zh-tw
Google Recaptcha v3 example demo
https://stackoverflow.com/questions/51507695/google-recaptcha-v3-example-demo
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
********************************************************************************
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=YOUR_SITE_KEY"></script> |
Default.aspx.cs
using Newtonsoft.Json.Linq;
using System;
using System.Net;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Login_Click(object sender, EventArgs e)
{
string secretKey = "YOUR_SECRET_KEY"; // 替換為您自己的reCAPTCHA v3秘鑰 YOUR_SECRET_KEY |
********************************************************************************
2024-03-28補
//不使用 Newtonsoft.Json.Linq,而是使用 .NET Framework 內建的 JavaScriptSerializer 來解析 JSON 字串。 protected void Button_Login_Click(object sender, EventArgs e) { string secretKey = "YOUR_SECRET_KEY"; // 替換為您自己的reCAPTCHA v3秘鑰 YOUR_SECRET_KEY string token = recaptchaToken.Value; // 取得令牌(token)值 using (var client = new WebClient()) { var response = client.DownloadString($"https://www.google.com/recaptcha/api/siteverify?secret={secretKey}&response={token}"); // 將回應轉換成字串 string responseString = Encoding.UTF8.GetString(Encoding.Default.GetBytes(response)); // 將 JSON 字串轉換成動態物件 dynamic result = new JavaScriptSerializer().DeserializeObject(responseString); // 取得 success 屬性值 bool success = result["success"]; if (success) { Label_MSG1.Text = "Google reCAPTCHA v3 驗證通過!"; } else { Label_MSG1.Text = "Google reCAPTCHA v3 驗證失敗,請重試!"; } } } |
相關



沒有留言:
張貼留言