forked from umby24/libMC.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinecraft_Net_Interaction.cs
More file actions
155 lines (120 loc) · 5.39 KB
/
Copy pathMinecraft_Net_Interaction.cs
File metadata and controls
155 lines (120 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.IO;
using Newtonsoft.Json.Linq;
namespace libMC.NET
{
public class Minecraft_Net_Interaction
{
public string[] Login(string username, string password) {
string json = "{\"agent\": {\"name\": \"minecraft\",\"version\": 1},\"username\": \"" + username + "\",\"password\": \"" + password + "\"}";
string accessToken = "", profileID = "", clientToken = "";
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create("https://authserver.mojang.com/authenticate");
wreq.Method = "POST";
wreq.ContentType = "application/json";
wreq.ContentLength = json.Length;
using (Stream stream = wreq.GetRequestStream()) {
stream.Write(Encoding.ASCII.GetBytes(json), 0, json.Length);
}
HttpWebResponse response;
String code;
try {
response = (HttpWebResponse)wreq.GetResponse();
code = new StreamReader(response.GetResponseStream()).ReadToEnd();
} catch {
// -- Error occured possible incorrect password.
return new[] { "", "", "" };
}
var root = JObject.Parse(code);
foreach (KeyValuePair<string, JToken> app in root) {
var appName = app.Key;
switch (appName) {
case "accessToken":
accessToken = app.Value.ToString();
break;
case "clientToken":
clientToken = app.Value.ToString();
break;
case "selectedProfile":
profileID = app.Value.First.First.ToString();
break;
}
}
return new string[] { accessToken, profileID, clientToken };
}
public bool VerifyName(string username, string accessToken, string selectedProfile, string ServerHash) {
string json = "{\"accessToken\": \"" + accessToken + "\",\"selectedProfile\": \"" + selectedProfile + "\",\"serverId\": \"" + ServerHash + "\"}";
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create("https://sessionserver.mojang.com/session/minecraft/join");
wreq.Method = "POST";
wreq.ContentType = "application/json";
wreq.ContentLength = json.Length;
using (Stream stream = wreq.GetRequestStream()) {
stream.Write(Encoding.ASCII.GetBytes(json), 0, json.Length);
}
try {
HttpWebResponse response = (HttpWebResponse)wreq.GetResponse();
string code = new StreamReader(response.GetResponseStream()).ReadToEnd();
} catch {
return false;
}
return true;
}
public string[] SessionRefresh(string accessToken, string clientToken) {
string json = "{\"accessToken\": \"" + accessToken + "\",\"clientToken\": \"" + clientToken + "\"}";
string accessTokenIn = "", clientTokenIn = "";
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create("https://authserver.mojang.com/refresh");
wreq.Method = "POST";
wreq.ContentType = "application/json";
wreq.ContentLength = json.Length;
using (Stream stream = wreq.GetRequestStream()) {
stream.Write(Encoding.ASCII.GetBytes(json), 0, json.Length);
}
HttpWebResponse response;
String code;
try {
response = (HttpWebResponse)wreq.GetResponse();
code = new StreamReader(response.GetResponseStream()).ReadToEnd();
} catch {
// -- Error occured possible incorrect credentials provided.
return new[] { "", "", "" };
}
var root = JObject.Parse(code);
foreach (KeyValuePair<string, JToken> app in root) {
var appName = app.Key;
switch (appName) {
case "accessToken":
accessTokenIn = app.Value.ToString();
break;
case "clientToken":
clientTokenIn = app.Value.ToString();
break;
}
}
return new[] { accessTokenIn, clientTokenIn};
}
public bool ValidateSession(string accessToken) {
string json = "{\"accessToken\": \"" + accessToken + "\"}";
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create("https://authserver.mojang.com/validate");
wreq.Method = "POST";
wreq.ContentType = "application/json";
wreq.ContentLength = json.Length;
using (Stream stream = wreq.GetRequestStream()) {
stream.Write(Encoding.ASCII.GetBytes(json), 0, json.Length);
}
try {
HttpWebResponse response = (HttpWebResponse)wreq.GetResponse();
string code = new StreamReader(response.GetResponseStream()).ReadToEnd();
if (code == "")
return true;
} catch {
return false;
}
return false;
}
}
}