C# 如何在运行时设置recaptcha密钥设置

C# 如何在运行时设置recaptcha密钥设置,c#,asp.net,webforms,recaptcha,C#,Asp.net,Webforms,Recaptcha,我正在实现来自谷歌的控制 我根据他们的示例和所有工作构建了一个简单的c#test项目。现在,我宁愿在运行时分配这些值,而不是在aspx页面中使用PublicKey和PrivateKey,因为它们很可能从web.config或数据库表中提取 我在页面加载中尝试了以下操作 protected void Page_Load(object sender, EventArgs e) { recaptcha.PublicKey = "<deleted for obvious r

我正在实现来自谷歌的控制

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

我在页面加载中尝试了以下操作

    protected void Page_Load(object sender, EventArgs e) {
        recaptcha.PublicKey = "<deleted for obvious reasons>";
        recaptcha.PrivateKey = "<ditto>";
    }
受保护的无效页面加载(对象发送方,事件参数e){
recaptcha.PublicKey=“”;
recaptcha.PrivateKey=“”;
}
但我得到一个错误,指出“reCAPTCHA需要配置一个公钥和私钥。”

我还尝试重写页面的oninit方法并在那里赋值,但没有成功


有什么建议吗?

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

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

希望有帮助。

另一种设置键值的方法,使用
,第一种方法)

Pro:如果您有多个声明,您只需将钥匙放在一个位置,即干燥原则

这种行为可以通过源代码看到:


搞砸了,似乎
setincodebehind
可以是任何字符串。谢谢,这工作得很好。我在这方面做了一段时间-是的-您必须在视图的控件中包含PublicKey和PrivateKey属性-但它们不需要正确的值。如果它们不在那里——当通过codebehind访问时——您得到的对象未设置为实例错误。使用“setincodebehind”方法最适合我。谢谢
public RecaptchaControl()
{
    this.publicKey = ConfigurationManager.AppSettings["RecaptchaPublicKey"];
    this.privateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"];
    ...