diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs
index 41427e98..503614c4 100644
--- a/MinecraftClient/Protocol/ProtocolHandler.cs
+++ b/MinecraftClient/Protocol/ProtocolHandler.cs
@@ -100,7 +100,7 @@ namespace MinecraftClient.Protocol
try
{
string result = "";
- string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + user + "\", \"password\": \"" + pass + "\" }";
+ string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + jsonEncode(user) + "\", \"password\": \"" + jsonEncode(pass) + "\" }";
int code = doHTTPSPost("authserver.mojang.com", "/authenticate", json_request, ref result);
if (code == 200)
{
@@ -204,5 +204,30 @@ namespace MinecraftClient.Protocol
}
else return 520; //Web server is returning an unknown error
}
+
+ ///
+ /// Encode a string to a json string.
+ /// Will convert special chars to \u0000 unicode escape sequences.
+ ///
+ /// Source text
+ /// Encoded text
+
+ private static string jsonEncode(string text)
+ {
+ StringBuilder result = new StringBuilder();
+ foreach (char c in text)
+ {
+ if (char.IsLetterOrDigit(c))
+ {
+ result.Append(c);
+ }
+ else
+ {
+ result.Append("\\u");
+ result.Append(((int)c).ToString("x4"));
+ }
+ }
+ return result.ToString();
+ }
}
}