Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

Login to MineCraft using C#

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...