Legacy Mojang Account

This api has been deprecated since Mojang closed their legacy login api.

Use Microsoft Xbox Account instead.

For legacy mojang account,

  • MLogin class provides methods to communicate with the Mojang auth server.

  • MLoginResponse class represents login result. Methods in MLogin class return this object.

  • MSession class represents player's session data, containing Username, UUID, and AccessToken.

Note: this document will help you to understand basic process of minecraft login.

Mojang Login

PremiumLogin() in CmlLibCoreSample

var login = new MLogin();

// TryAutoLogin() reads the login cache file and check validation.
// If the cached session is invalid, it refreshes the session automatically.
// Refreshing the session doesn't always succeed, so you have to handle this.
Console.WriteLine("Attempting to automatically log in.");
var response = login.TryAutoLogin();

if (!response.IsSuccess) // if cached session is invalid and failed to refresh token
{
    Console.WriteLine("Auto login failed: {0}", response.Result.ToString());

    Console.WriteLine("Input your Mojang email: ");
    var email = Console.ReadLine();
    Console.WriteLine("Input your Mojang password: ");
    var pw = Console.ReadLine();

    response = login.Authenticate(email, pw);

    if (!response.IsSuccess)
    {
        // session.Message contains a detailed error message. It can be null or an empty string.
        Console.WriteLine("failed to login. {0} : {1}", response.Result.ToString(), response.ErrorMessage);
        Console.ReadLine();
        Environment.Exit(0);
    }
}

// The result Session
MSession session = response.Session;

// var launchOption = new MLaunchOption()
// {
//      Session = session,
//      // launch options
// };

MLogin

Provides methods to communicate with the Mojang auth server and cache game session. All methods return MLoginResult. You can get the result of login and result session from MLoginResponse. This class fully implments Yggdrasil authentication scheme.

Constructor

public MLogin()

Initialize object with default login cache file path. Default path : Path.Combine(MinecraftPath.GetOSDefaultPath(), "logintoken.json")

public MLogin(string sessionCacheFilePath)

Initializes object and sets SessionCacheFilePath.

Properties

SessionCacheFilePath

Type: string

SessionCacheFilePath

SaveSession

Save session data to SessionCacheFilePath if this true. Default value is true.

Methods

public MSession ReadSessionCache()

Returns session from cache file.

public MLoginResponse Authenticate(string id, string pw)

Login with Mojang email and password, with cached clientToken.

public MLoginResponse Authenticate(string id, string pw, string clientToken)

Login with Mojang email and password.

public MLoginResponse TryAutoLogin()

Checks validation of cached session and refresh session if it is not valid session.

public MLoginResponse TryAutoLogin(MSession session)

Checks validation of the specified session and refresh session if it is not valid session.

public MLoginResponse Refresh()

Refresh session using cached session.

public MLoginResponse Refresh(MSession session)

Refresh the specified session.

public MLoginResponse Validate()

Validate session with cached session.

public MLoginResponse Validate(MSession session)

Validate the specified session.

public void DeleteTokenFile()

Delete cached session file. This is the easiest way to logout.

public bool Invalidate()

Logout with cached session file.

public bool Invalidate(MSession session)

Logout the specified session.

public bool Signout(string id, string pw)

Logout using Mojang email and password.

MLoginResponse

Indicates the login response.

Properties

IsSuccess

Type: bool

Returns true if Result is MLoginResult.Success.

Result

Type: MLoginResponse

Login Result. If this property is not MLoginResult.Success, then Session will be null.

Session

Type: Login and Sessions

Result session.

ErrorMessage

Type: string

Error message. It is set when the Result property is not MLoginResult.Success. This property can be empty or a null string when the login result is not successful.

MLoginResult

Indicates the login result.

Fields

Success

Login successful.

BadRequest

WrongAccount

NeedLogin

UnknownError

NoProfile

User doesn't purchase minecraft.

Last updated