Login to MineCraft using C#

坚强是说给别人听的谎言 提交于 2019-12-13 10:05:34

问题


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);

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.


回答1:


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)

Code:

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;
        }
    }
}

Declare these aswell:

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;

And if you haven't already, refrence System.Web.Extentions I tested this with C# winforms and it works :)

Thanks,

DMP9




回答2:


The login server is now https://authserver.mojang.com/authenticate, and it uses JSON-formatted info.

Use this format for the JSON request:

{"agent": { "name": "Minecraft", "version": 1 }, "username": "example", "password": "hunter2"}

Here is a full implementation for logging in.



来源:https://stackoverflow.com/questions/26331163/login-to-minecraft-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!