我正在研究一种认证方法,有两个文本字段分别命名为“用户名”和“密码”。 我想实现这样一个功能,当用户输入他们的用户名和密码时,将其存储到文本文件中。因此,在他们再次登录时,它会从文本文件中读取用户名和密码以进行登录。 我该如何做到这一点? 谢谢:D
我正在研究一种认证方法,有两个文本字段分别命名为“用户名”和“密码”。 我想实现这样一个功能,当用户输入他们的用户名和密码时,将其存储到文本文件中。因此,在他们再次登录时,它会从文本文件中读取用户名和密码以进行登录。 我该如何做到这一点? 谢谢:D
可以将内容保存到文本文件中(使用AIR中的File类),但这并不是一个好的方法。相反,您应该查看SharedObject类。
快速示例:
var sharedObject:SharedObject = SharedObject.getLocal("userInfo"); //this will look for a shared object with the id userInfo and create a new one if it doesn't exist
sharedObject.data.userName = "Some username";
sharedObject.data.password= "Some password"; //it's really not a good idea to save a password like this
sharedObject.flush(); //saves everything out
现在要在代码的其他地方获取您的数据
var sharedObject:SharedObject = SharedObject.getLocal("userInfo");
trace(sharedObject.data.userName);
trace(sharedObject.data.password);
这个对象被保存在用户的计算机上,非常类似于浏览器的cookie。
现在将密码以明文形式保存到这个对象中并不是一个好主意。更好的方式是在服务器上验证登录信息并在此对象中存储某种类型的会话ID。
伪代码如下:
function validateLogin(){
var sessionID = server->checkLogin(username, password); //returns a string if authed, nothing if not
if(sessionID){
sharedObject->sessionID = sessionID;
} else {
//bad login
}
}
更多阅读:
http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html
http://www.republicofcode.com/tutorials/flash/as3sharedobject/