Skip to content

Login and Sessions

AlphaBs edited this page Jun 2, 2022 · 6 revisions

MLogin and MSession

This document describes how to get or create Minecraft sessions using CmlLib.Core.

To connect to online-mode server, you should obtain player's session data. The game session data contains player's username, UUID, and accessToken.

There are some ways to obtain game session using this library:

  • Mojang Login
  • Microsoft Xbox Login
  • Offline Login

This library provides some classes related game session and login process:

  • MLogin class provides methods to communicate with the Mojang auth server.
  • MLoginResult class represents login result. Methods in MLogin class return this object.
  • MSession class represents player's session data, containing Username, UUID, and AccessToken.

After obtaining a session data, you should set the MLaunchOption.Session property to an MSession instance.

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

Mojang Login

The basic login process is:
img

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

Microsoft Xbox Login

go to Microsoft Xbox Login

Offline Login

This session cannot be used in online-mode server or realm.

MSession session = MSession.GetOfflineSession("username");

Creating your own session data

MSession session = new MSession("username", "accesstoken", "uuid");

MLogin

Provides methods to communicate with the Mojang auth server and cache game session.
All methods return MLoginResponse. 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.

MSession

Represents a Minecraft session.

Constructor

public MSession()

Creates an empty session.

public MSession(string username, string accesstoken, string uuid)

Creates an MSession with the specified Username, AccessToken, and UUID properties.

Properties

Username

Type: string

UUID

Type: string

AccessToken

Type: string

ClientToken

Type: string

Methods

public bool CheckIsValid()

Return true if Username, AccessToken, UUID is not null or empty.

public static MSession GetOfflineSession(string username)

Creates a new MSession and returns it. Username=username, AccessToken="access_token", UUID="user_uuid"

MLoginResponse

Indicates the login response.

Properties

IsSuccess

Type: bool

Returns true if Result is MLoginResult.Success.

Result

Type: MLoginResult

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

Session

Type: MSession

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.

Clone this wiki locally