// ---------------------------------------------------------------------------- // // Photon Cloud Account Service - Copyright (C) 2012 Exit Games GmbH // // // Provides methods to register a new user-account for the Photon Cloud and // get the resulting appId. // // developer@exitgames.com // ---------------------------------------------------------------------------- //#define PHOTON_VOICE using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System; using System.Collections.Generic; using System.IO; using System.Net; using Newtonsoft.Json; public class AccountService { private const string ServiceUrl = "https://service.exitgames.com/AccountExt/AccountServiceExt.aspx"; private Action registrationCallback; // optional (when using async reg) public string Message { get; private set; } // msg from server (in case of success, this is the appid) protected internal Exception Exception { get; set; } // exceptions in account-server communication public string AppId { get; private set; } public string AppId2 { get; private set; } public int ReturnCode { get; private set; } // 0 = OK. anything else is a error with Message public enum Origin : byte { ServerWeb = 1, CloudWeb = 2, Pun = 3, Playmaker = 4 }; /// /// Creates a instance of the Account Service to register Photon Cloud accounts. /// public AccountService() { WebRequest.DefaultWebProxy = null; ServicePointManager.ServerCertificateValidationCallback = Validator; } public static bool Validator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors) { return true; // any certificate is ok in this case } /// /// Attempts to create a Photon Cloud Account. /// Check ReturnCode, Message and AppId to get the result of this attempt. /// /// Email of the account. /// Marks which channel created the new account (if it's new). /// Defines which type of Photon-service is being requested. public void RegisterByEmail(string email, Origin origin, string serviceType = null) { this.registrationCallback = null; this.AppId = string.Empty; this.AppId2 = string.Empty; this.Message = string.Empty; this.ReturnCode = -1; string result; try { WebRequest req = HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin, serviceType)); HttpWebResponse resp = req.GetResponse() as HttpWebResponse; // now read result StreamReader reader = new StreamReader(resp.GetResponseStream()); result = reader.ReadToEnd(); } catch (Exception ex) { this.Message = "Failed to connect to Cloud Account Service. Please register via account website."; this.Exception = ex; return; } this.ParseResult(result); } /// /// Attempts to create a Photon Cloud Account asynchronously. /// Once your callback is called, check ReturnCode, Message and AppId to get the result of this attempt. /// /// Email of the account. /// Marks which channel created the new account (if it's new). /// Defines which type of Photon-service is being requested. /// Called when the result is available. public void RegisterByEmailAsync(string email, Origin origin, string serviceType, Action callback = null) { this.registrationCallback = callback; this.AppId = string.Empty; this.AppId2 = string.Empty; this.Message = string.Empty; this.ReturnCode = -1; try { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin, serviceType)); req.Timeout = 5000; req.BeginGetResponse(this.OnRegisterByEmailCompleted, req); } catch (Exception ex) { this.Message = "Failed to connect to Cloud Account Service. Please register via account website."; this.Exception = ex; if (this.registrationCallback != null) { this.registrationCallback(this); } } } /// /// Internal callback with result of async HttpWebRequest (in RegisterByEmailAsync). /// /// private void OnRegisterByEmailCompleted(IAsyncResult ar) { try { HttpWebRequest request = (HttpWebRequest)ar.AsyncState; HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse; if (response != null && response.StatusCode == HttpStatusCode.OK) { // no error. use the result StreamReader reader = new StreamReader(response.GetResponseStream()); string result = reader.ReadToEnd(); this.ParseResult(result); } else { // a response but some error on server. show message this.Message = "Failed to connect to Cloud Account Service. Please register via account website."; } } catch (Exception ex) { // not even a response. show message this.Message = "Failed to connect to Cloud Account Service. Please register via account website."; this.Exception = ex; } if (this.registrationCallback != null) { this.registrationCallback(this); } } /// /// Creates the service-call Uri, escaping the email for security reasons. /// /// Email of the account. /// 1 = server-web, 2 = cloud-web, 3 = PUN, 4 = playmaker /// Defines which type of Photon-service is being requested. Options: "", "voice", "chat" /// Uri to call. private Uri RegistrationUri(string email, byte origin, string serviceType) { if (serviceType == null) { serviceType = string.Empty; } string emailEncoded = Uri.EscapeDataString(email); string uriString = string.Format("{0}?email={1}&origin={2}&serviceType={3}", ServiceUrl, emailEncoded, origin, serviceType); return new Uri(uriString); } /// /// Reads the Json response and applies it to local properties. /// /// private void ParseResult(string result) { if (string.IsNullOrEmpty(result)) { this.Message = "Server's response was empty. Please register through account website during this service interruption."; return; } Dictionary values = JsonConvert.DeserializeObject>(result); if (values == null) { this.Message = "Service temporarily unavailable. Please register through account website."; return; } int returnCodeInt = -1; string returnCodeString = string.Empty; string message; string messageDetailed; values.TryGetValue("ReturnCode", out returnCodeString); values.TryGetValue("Message", out message); values.TryGetValue("MessageDetailed", out messageDetailed); int.TryParse(returnCodeString, out returnCodeInt); this.ReturnCode = returnCodeInt; if (returnCodeInt == 0) { // returnCode == 0 means: all ok. message is new AppId this.AppId = message; #if PHOTON_VOICE this.AppId2 = messageDetailed; #endif } else { // any error gives returnCode != 0 this.AppId = string.Empty; #if PHOTON_VOICE this.AppId2 = string.Empty; #endif this.Message = message; } } }