如何在运行时设置验证码密钥设置

发布于 2024-09-06 21:10:42 字数 580 浏览 6 评论 0原文

我正在实施来自 google 的 recaptcha 控件。

我从他们的示例和所有工作中构建了一个简单的 C# 测试项目。现在,我宁愿在运行时分配这些值,而不是将 PublicKey 和 PrivateKey 放在 aspx 页面中,因为它们很可能是从 web.config 或数据库表中提取的。

我在 Page_Load 中尝试了以下操作

    protected void Page_Load(object sender, EventArgs e) {
        recaptcha.PublicKey = "<deleted for obvious reasons>";
        recaptcha.PrivateKey = "<ditto>";
    }

,但收到一条错误消息,指出“reCAPTCHA 需要使用公钥和私钥进行配置”。

我还尝试覆盖页面的 oninit 方法并在那里分配值,但没有什么乐趣。

关于这需要去哪里有什么想法吗?

I'm implementing the recaptcha control from google.

I built a simple c# test project from their example and all works. Now, instead of having the PublicKey and PrivateKey in the aspx page, I'd rather assign these values at run time as they will most likely be pulled from either the web.config or a database table.

I tried the following in the Page_Load

    protected void Page_Load(object sender, EventArgs e) {
        recaptcha.PublicKey = "<deleted for obvious reasons>";
        recaptcha.PrivateKey = "<ditto>";
    }

but I get an error stating "reCAPTCHA needs to be configured with a public & private key."

I also tried overriding the oninit method of the page and assigning the values there, but no joy.

Any ideas on where this needs to go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2024-09-13 21:10:42

尝试在标记中使用 setincodebehind 的值,如下所示:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="setincodebehind" PrivateKey="setincodebehind" ... />

这应该可以让您正确设置代码隐藏中的键。还有其他几种方法可以做到这一点。例如,您可以从静态类中获取值,如下所示:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%= RecaptchaSettings.PublicKey %>" 
  PrivateKey="<%= RecaptchaSettings.PrivateKey %>" ... />

其中 RecaptchaSettings 是您提供的类。或者,您可以将密钥放入 web.config 的 appSettings 部分,并按如下方式访问它们:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%$appSettings:RecaptchaPublicKey %>" 
  PrivateKey="<%$appSettings:RecaptchaPrivateKey %>" ... />

希望有帮助。

Try using the value of setincodebehind in your tag, like this:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="setincodebehind" PrivateKey="setincodebehind" ... />

That should let you set the keys in the codebehind properly. There are a couple of other ways to do it as well. For example, you can get the values from a static class like this:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%= RecaptchaSettings.PublicKey %>" 
  PrivateKey="<%= RecaptchaSettings.PrivateKey %>" ... />

Where RecaptchaSettings is a class you provide. Or, you could put the keys into an appSettings section of your web.config, and access them like this:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%$appSettings:RecaptchaPublicKey %>" 
  PrivateKey="<%$appSettings:RecaptchaPrivateKey %>" ... />

Hope that helps.

谈下烟灰 2024-09-13 21:10:42

设置键值的另一种方法是使用 RecaptchaPublicKeyRecaptchaPrivateKey。这些值将自动使用,除非在控件声明期间被覆盖(mjd79 的回答,第一种方式)。

优点:如果你有多个声明,你只需要把键放在一个地方,DRY原则。

通过源代码可以看到此行为,RecaptchaControl.cs,第 135 行-...

public RecaptchaControl()
{
    this.publicKey = ConfigurationManager.AppSettings["RecaptchaPublicKey"];
    this.privateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"];
    ...

Another way to set key values, use <appSettings> keys RecaptchaPublicKey and RecaptchaPrivateKey. These values will be used automatically unless it is overridden during control declaration (mjd79's answer, first way).

Pro: if you have multiple declaration, you only need to keep the keys in one place, DRY principle.

This behaviour can be seen through the source code, RecaptchaControl.cs, line 135-...:

public RecaptchaControl()
{
    this.publicKey = ConfigurationManager.AppSettings["RecaptchaPublicKey"];
    this.privateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"];
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文