如何在运行时设置重新访问密钥设置

Not*_*tMe 8 c# asp.net webforms recaptcha

我正在从谷歌实施recaptcha控件.

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

我在Page_Load中尝试了以下内容

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

但我收到一条错误消息,指出"需要使用公钥和私钥配置reCAPTCHA".

我也尝试重写页面的oninit方法并在那里分配值,但没有快乐.

关于这需要去哪里的任何想法?

mat*_*att 16

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

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="setincodebehind" PrivateKey="setincodebehind" ... />
Run Code Online (Sandbox Code Playgroud)

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

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%= RecaptchaSettings.PublicKey %>" 
  PrivateKey="<%= RecaptchaSettings.PrivateKey %>" ... />
Run Code Online (Sandbox Code Playgroud)

RecaptchaSettings你提供的课程在哪里?或者,您可以将密钥放入appSettingsweb.config的一部分,并像这样访问它们:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%$appSettings:RecaptchaPublicKey %>" 
  PrivateKey="<%$appSettings:RecaptchaPrivateKey %>" ... />
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.