/// <summary> /// Registers a new account /// </summary> public static bool RegisterAccount(string Username, string Password, string Email) { string msg; Status.RegisterRequestObject request = new Status.RegisterRequestObject(); request.Username = Username; request.PasswordHash = Md5.Hash(Password); request.Email = Email; switch (AccountServer.RegisterAccount(request, CurrentUrl)) { case Status.RegistrationStatusCode.Ok: MessageBox.Show("Account created!", "Register Request"); return(true); case Status.RegistrationStatusCode.MalformedData: msg = "Error: malformed username/password"; MessageBox.Show(string.IsNullOrWhiteSpace(AccountServer.Reason) ? msg : "Error: " + AccountServer.Reason, "Register Request"); break; case Status.RegistrationStatusCode.UsernameTaken: msg = "Error: Username is taken."; MessageBox.Show(string.IsNullOrWhiteSpace(AccountServer.Reason) ? msg : "Error: " + AccountServer.Reason, "Register Request"); break; case Status.RegistrationStatusCode.EmailTaken: msg = "Error: Email is already used."; MessageBox.Show(string.IsNullOrWhiteSpace(AccountServer.Reason) ? msg : "Error: " + AccountServer.Reason, "Register Request"); break; case Status.RegistrationStatusCode.WeakCredentials: msg = "too short/invalid characters/invalid email"; MessageBox.Show("Error: Credentials are invalid.\n\r(" + (string.IsNullOrWhiteSpace(AccountServer.Reason) ? msg : AccountServer.Reason) + ")", "Register Request"); break; case Status.RegistrationStatusCode.ServerError: msg = "Server error, could not connect. Is your firewall enabled?"; MessageBox.Show(string.IsNullOrWhiteSpace(AccountServer.Reason) ? msg : "Error: " + AccountServer.Reason, "Register Request"); break; case Status.RegistrationStatusCode.NoResponse: msg = "Server error, received no response from the server."; MessageBox.Show(string.IsNullOrWhiteSpace(AccountServer.Reason) ? msg : "Error: " + AccountServer.Reason, "Register Request"); break; } return(false); }
/// <summary> /// Sends a register request to our account server /// </summary> public static Status.RegistrationStatusCode RegisterAccount(Status.RegisterRequestObject requestModel, string RegisterUrl) { if (requestModel == null) { throw new ArgumentNullException("requestModel"); } byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestModel)); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(RegisterUrl); httpWebRequest.Method = "PUT"; httpWebRequest.ContentType = "application/json"; httpWebRequest.ContentLength = bytes.Length; try { httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length); } catch (WebException) { return(Status.RegistrationStatusCode.ServerError); } try { using (StreamReader streamReader = new StreamReader(httpWebRequest.GetResponse().GetResponseStream())) { streamReader.ReadToEnd(); return(Status.RegistrationStatusCode.Ok); } } catch (WebException ex) { if ((HttpWebResponse)ex.Response == null) { return(Status.RegistrationStatusCode.NoResponse); } switch (((HttpWebResponse)ex.Response).StatusCode) { case HttpStatusCode.Forbidden: Reason = ((HttpWebResponse)ex.Response).StatusDescription; return(Status.RegistrationStatusCode.UsernameTaken); case HttpStatusCode.Conflict: Reason = ((HttpWebResponse)ex.Response).StatusDescription; return(Status.RegistrationStatusCode.EmailTaken); case HttpStatusCode.NotAcceptable: Reason = ((HttpWebResponse)ex.Response).StatusDescription; return(Status.RegistrationStatusCode.WeakCredentials); case HttpStatusCode.InternalServerError: Reason = ((HttpWebResponse)ex.Response).StatusDescription; return(Status.RegistrationStatusCode.ServerError); case HttpStatusCode.Created: return(Status.RegistrationStatusCode.Ok); case HttpStatusCode.BadRequest: return(Status.RegistrationStatusCode.MalformedData); default: return(Status.RegistrationStatusCode.ServerError); } } }