Các bước tích hợp reCaptcha vào website in C#

0 thích 0 không thích
1 lượt xem
đã hỏi 3 Tháng 4, 2023 trong Lập trình C# bởi nguyenthao (9,040 điểm)
(Another way : https://github.com/tanveery/recaptcha-net)

1. Get secret key and site key from : https://www.google.com/recaptcha/admin

2. Add them to web config :
    <add key="recaptcha-secret-key" value="...[secret key]" />
    <add key="recaptcha-public-key" value="...[public-key]" />

3. Install Package :
    PM > Install-Package ReCaptcha-AspNet

4. Optional :
    You can change language in web.config : (https://developers.google.com/recaptcha/docs/language)
        <add key="recaptcha-language-key" value="[language-code]" />
    Or via C# code :
        string publicKey = "...[public-key]";
        string secretKey = "...[secret-key]";
        ReCaptcha.Configure(publicKey, secretKey);

        // Optional, select a default language:
        string publicKey = "...[public-key]";
        string secretKey = "...[secret-key]";
        ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage.German;
        ReCaptcha.Configure(publicKey, secretKey, defaultLanguage);

5. Add this line in <head> tag :
    <script src='https://www.google.com/recaptcha/api.js'></script>
6. and this line (given by google) in bottom of your form :
    <div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"></div>

7. In button click enevn write this code:
        string userResponse = Request.Params["g-recaptcha-response"];
        bool validCaptcha = ReCaptcha.ValidateCaptcha(userResponse);
        if (validCaptcha)
        {
            // Real User, validated !
            Response.Write("Passed");
        }
        else
        {
            // Bot Attack, non validated !
            Response.Write("You are robot");
        }
Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...