Tay*_*ift 3 actionscript actionscript-3
我正在研究一种身份验证方法,我有两个名为'username'和'pass'的文本字段,我希望这样做,以便当用户输入用户名和密码时,该信息将存储到文本文件中.因此,当他们重新登录时,它会从该文本文件中读取用户名和密码以进行登录.我该怎么做?感谢: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
Run Code Online (Sandbox Code Playgroud)
一旦掌握了sharedObject
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
Run Code Online (Sandbox Code Playgroud)
现在,在代码中的其他位置恢复数据
var sharedObject:SharedObject = SharedObject.getLocal("userInfo");
trace(sharedObject.data.userName);
trace(sharedObject.data.password);
Run Code Online (Sandbox Code Playgroud)
此对象本地保存到用户计算机.它与浏览器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
}
}
Run Code Online (Sandbox Code Playgroud)
更多阅读:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html
http://www.republicofcode.com/tutorials/flash/as3sharedobject/