如何在运行时设置验证码密钥设置
我正在实施来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在标记中使用
setincodebehind
的值,如下所示:这应该可以让您正确设置代码隐藏中的键。还有其他几种方法可以做到这一点。例如,您可以从静态类中获取值,如下所示:
其中
RecaptchaSettings
是您提供的类。或者,您可以将密钥放入 web.config 的appSettings
部分,并按如下方式访问它们:希望有帮助。
Try using the value of
setincodebehind
in your tag, like this: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:
Where
RecaptchaSettings
is a class you provide. Or, you could put the keys into anappSettings
section of your web.config, and access them like this:Hope that helps.
设置键值的另一种方法是使用
键RecaptchaPublicKey
和RecaptchaPrivateKey
。这些值将自动使用,除非在控件声明期间被覆盖(mjd79 的回答,第一种方式)。优点:如果你有多个声明,你只需要把键放在一个地方,DRY原则。
通过源代码可以看到此行为,RecaptchaControl.cs,第 135 行-...:
Another way to set key values, use
<appSettings>
keysRecaptchaPublicKey
andRecaptchaPrivateKey
. 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-...: