野狗(wilddog)key-value存储js版试用,结合asp.net。野狗提供的是key-value的快速存储,类似redis这种。
1.读取数据
首先引用js jdk
<script src = "https://cdn.wilddog.com/js/client/current/wilddog.js" ></script>
再创建引用
var ref = new Wilddog("https://xxxx.wilddogio.com/"); //XXXX为你的二级域名
读取数据
var name = $("#TextBox1").val(); ref.child(name).on("value", function (datasnapshot) { alert(datasnapshot.val()); });
html内容
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <input id="Button3" type="button" value="写入" onclick="WriteInfo()" /> <input id="Button4" type="button" value="读取" onclick="ReadInfo()" />
2.数据的写入
function WriteInfo() { var name = $("#TextBox1").val(); var value = $("#TextBox2").val(); ref.child(name).set(value); alert("success!"); }
3.安全认证
var authData = ref.getAuth(); function authHandler(error, authData) { if (error) { //console.log("Login Failed!", error); alert("没有权限访问!"); } else { //console.log("Authenticated successfully with payload:", authData); } } ref.authWithPassword({ email: '<%=userName %>', password: '<%=pwd %>' }, authHandler);
至于email和password的验证,各个后台语言有不同的策略,大致思路应该差不多,下面是C#版
public string userName = "xxx", pwd = "xxx"; protected void Page_Load(object sender, EventArgs e) { //可以存放在数据库,读取出来赋值 }
安全认证还要结合规则表达式的使用以及终端认证
{ "rules": { ".read": "auth != null", ".write": true } }
参考文档:
https://z.wilddog.com/web/quickstart
https://z.wilddog.com/rule/quickstart
https://z.wilddog.com/web/auth