So basically the script I am working on sends some data to a web server, and the server responds with a JSON reply to tell the app if it has logged it, but I can’t seem to get it working. Here is the related code:
public class LoginResponse
{
public bool status {get;set;}
public string message {get;set;}
}
public class Response
{
public List<LoginResponse> data {get;set;}
}
string response=download.text;
Response loginResponse = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Response>(response);
But I keep getting this error:
I have tried adding
Using System.Collections.Generic;
to my code, but when I do I get TONS more errors, and when I try accessing List by System.Collection.Generic.List the same thing happens!!
Any suggestions?
Here is the entire code of the script (with the key and IV edited out though):
using UnityEngine;
using System.Collections;
using System.Security.Cryptography;
public class LoginResponse
{
public bool status {get;set;}
public string message {get;set;}
}
public class Response
{
public List<LoginResponse> data {get;set;}
}
public class Login : MonoBehaviour {
public Transform userfield;
public Transform pass1field;
public Transform pass2field;
public Transform questanfield;
public UILabel secret_question;
private bool loggedin=false;
private string user,pass1,pass2,answer;
private string login_url="REMOVED"; //set to login script
private const string key="REMOVED";
private const string iv="REMOVED";
void OnClick(){
user=EncryptRJ256(key,iv,userfield.GetComponent<UIInput>().text);
pass1=EncryptRJ256(key,iv,pass1field.GetComponent<UIInput>().text);
pass2=EncryptRJ256(key,iv,pass2field.GetComponent<UIInput>().text);
answer=EncryptRJ256(key,iv,questanfield.GetComponent<UIInput>().text);
Debug.Log("User:"+user);
Debug.Log("Pass1:"+pass1);
Debug.Log("Pass2:"+pass2);
Debug.Log("Answer:"+answer);
var webform=new WWWForm();
webform.AddField("user",user);
webform.AddField("pass1",pass1);
webform.AddField("pass2",pass2);
webform.AddField("answer",answer);
webform.AddField("iv",iv);
WWW download=new WWW(login_url,webform);
//yield download;
string response=download.text;
Response loginResponse = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Response>(response);
//Ignore this section for now
if(response=="true"){
loggedin=true;
}
}
void OnUpdate(){
if(loggedin){
Application.LoadLevel("Main");
}
}
//Encryption Functions
string DecryptRJ256(string prm_key, string prm_iv, string prm_text_to_decrypt) {
var sEncryptedString = prm_text_to_decrypt;
var myRijndael = new RijndaelManaged() {
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 256,
BlockSize = 256
};
var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);
var decryptor = myRijndael.CreateDecryptor(key, IV);
var sEncrypted = Convert.FromBase64String(sEncryptedString);
var fromEncrypt = new byte[sEncrypted.Length];
var msDecrypt = new MemoryStream(sEncrypted);
var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return (Encoding.ASCII.GetString(fromEncrypt));
}
string EncryptRJ256(string prm_key, string prm_iv, string prm_text_to_encrypt) {
var sToEncrypt = prm_text_to_encrypt;
var myRijndael = new RijndaelManaged() {
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 256,
BlockSize = 256
};
var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);
var encryptor = myRijndael.CreateEncryptor(key, IV);
var msEncrypt = new MemoryStream();
var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
var toEncrypt = Encoding.ASCII.GetBytes(sToEncrypt);
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
var encrypted = msEncrypt.ToArray();
return (Convert.ToBase64String(encrypted));
}
}
1 Like
Just wanted to say that I finally got it working. For anyone else that happens across this thread use http://wiki.unity3d.com/index.php/SimpleJSON and line 54 becomes
1 Like