使用C#登录MineCraft [英] Login to MineCraft using C#
问题描述
我正在尝试为自己和一些朋友创建一个简单的自定义Minecraft启动器.我不需要启动Minecraft的代码,只需登录实际的代码行即可.例如,据我所知,您曾经能够使用:
I'm trying to create a simple custom minecraft launcher for myself and some friends. I don't need the code to start minecraft, just the actual line of code to login. For example, to my knowledge, you used to be able to use:
string netResponse = httpGET("https://login.minecraft.net/session?name=<USERNAME>&session=<SESSION ID>" + username + "&password=" + password + "&version=" + clientVer);
我知道不再有 https://login.minecraft.net ,表示此代码将无法正常工作.这是我需要继续的所有内容,仅是连接登录的地方以及要包含的变量.谢谢,如果需要任何其他信息,请发表评论.
I'm aware there is no longer a https://login.minecraft.net, meaning this code won't work. This is about all I need to continue, only the place to connect to login, and the variables to include. Thanks, if any additional info is needed, give a comment.
推荐答案
您需要向 https发送JSON POST请求://authserver.mojang.com/authenticate ,这是我获取访问令牌(您可以用来玩游戏)的方法
You need to make a JSON POST request to https://authserver.mojang.com/authenticate and heres my method of getting an access token (which you can use to play the game)
代码:
string ACCESS_TOKEN;
public string GetAccessToken()
{
return ACCESS_TOKEN;
}
public void ObtainAccessToken(string username, string password)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://authserver.mojang.com/authenticate");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"agent\":{\"name\":\"Minecraft\",\"version\":1},\"username\":\""+username+"\",\"password\":\""+password+"\",\"clientToken\":\"6c9d237d-8fbf-44ef-b46b-0b8a854bf391\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
ACCESS_TOKEN = result;
}
}
}
还要声明以下内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
如果还没有,请参考System.Web.Extensions 我用C#winforms对此进行了测试,并且可以::
And if you haven't already, refrence System.Web.Extentions I tested this with C# winforms and it works :)
谢谢
DMP9
这篇关于使用C#登录MineCraft的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!