JELoginHandler

Login, logout, account managements.

Creating JELoginHandler instance

var loginHandler = JELoginHandlerBuilder.BuildDefault();

For more detailed initialization, which includes specifying how accounts are stored, setting up HttpClient, and more, please refer to JELoginHandlerBuilder.

Basic Authentication

var session = await loginHandler.Authenticate();
// var session = await loginHandler.Authenticate(selectedAccount, cancellationToken);

This method tries Authenticating with the Most Recent Account first and if it fails, tries Authenticating with New Account.

Authenticating with New Account

var session = await loginHandler.AuthenticateInteractively();
// var session = await loginHandler.AuthenticateInteractively(selectedAccount, cancellationToken);

Add a new account to sign in. Show the user the Microsoft OAuth page to enter their Microsoft account.

This method uses Microsoft WebView2 for displaying Microsoft OAuth login page. You must know that:

If you don't want to use WebView2, you can use XboxAuthNet.Game.Msal instead.

Authenticating with the Most Recent Account

var session = await loginHandler.AuthenticateSilently();
// var session = await loginHandler.AuthenticateSilently(selectedAccount, cancellationToken);

Using the saved account information of the most account, log in.

  • If the user is already logged in, this method returns the logged in information immediately.

  • If the user's login information has expired, try to refresh it. No user interaction nor webview is required during this process.

  • If there is no saved login information or if refresh failed, an MicrosoftOAuthException will be thrown. In this case you should authenticate again using new account methods like Authenticating with New Account.

List Accounts

var accounts = loginHandler.AccountManager.GetAccounts();
foreach (var account in accounts)
{
    if (account is not JEGameAccount jeAccount)
        continue;
    Console.WriteLine("Identifier: " + jeAccount.Identifier);
    Console.WriteLine("LastAccess: " + jeAccount.LastAccess);
    Console.WriteLine("Gamertag: " + jeAccount.XboxTokens?.XstsToken?.XuiClaims?.Gamertag);
    Console.WriteLine("Username: " + jeAccount.Profile?.Username);
    Console.WriteLine("UUID: " + jeAccount.Profile?.UUID);
}

After a successful login, the account is saved. Above code list all saved account lists.

Select Account

Select account by index number:

var accounts = loginHandler.AccountManager.GetAccounts();
var selectedAccount = accounts.ElementAt(1);

All account has unique string to identifiy them. Select account by identifier:

var accounts = loginHandler.AccountManager.GetAccounts();
var selectedAccount = accounts.GetAccount("Identifier");

Select account by JE username:

var accounts = loginHandler.AccountManager.GetAccounts();
var selectedAccount = accounts.GetJEAccountByUsername("username");

Authenticating with the Selected Account

var accounts = loginHandler.AccountManager.GetAccounts();
var selectedAccount = accounts.ElementAt(1);
var session = await loginHandler.Authenticate(selectedAccount);

Load account list and authenticate with second account (index number 1).

Signing out from the most Recent Account

Signout method does not clear WebView2 browser cache. For clearing it, call SignoutWithBrowser instead.

await loginHandler.Signout();
// await loginHandler.SignoutWithBrowser();

Signing out from the Selected Account

Signout method does not clear WebView2 browser cache. For clearing it, call SignoutWithBrowser instead.

var accounts = loginHandler.AccountManager.GetAccounts();
var selectedAccount = accounts.ElementAt(1);
await loginHandler.Signout(selectedAccount);
// await loginHandler.SignoutWithBrowser();

Load account list and sign out from second account (index number 1).

Authenticating with More Options

using XboxAuthNet.Game;

// 1. Create Authenticator 
var authenticator = loginHandler.CreateAuthenticator(account, default);

// 2. OAuth
authenticator.AddMicrosoftOAuthForJE(oauth => oauth.Interactive());

// 3. XboxAuth
authenticator.AddXboxAuthForJE(xbox => xbox.Basic());

// 4. JEAuthenticator
authenticator.AddJEAuthenticator();

// Execute authenticator
var session = await authenticator.ExecuteForLauncherAsync();

The login process has four main steps. There are many methods to customize authentication flow in each main step. You must select only one method for each step.

1. Create Authenticator

var authenticator = loginHandler.CreateAuthenticator(account, default);

Initialize Authenticator instance with the specific account to login. Another ways to initialize this:

var authenticator = loginHandler.CreateAuthenticatorWithNewAccount(default);

Initialize Authenticator with new empty account.

var authenticator = loginHandler.CreateAuthenticatorWithDefaultAccount(default);

Initialize Authenticator with the most recent account.

You can pass CancellationToken instead of default.

2. OAuth

authenticator.AddMicrosoftOAuthForJE(oauth => oauth.Interactive());

// above code is same as
// authenticator.AddMicrosoftOAuth(JELoginHandler.DefaultMicrosoftOAuthClientInfo, oauth => oauth.Interactive());

// another OAuth method can be:
// 1) authenticator.AddForceMicrosoftOAuthForJE(oauth => oauth.Interactive());
// 2) authenticator.AddMicrosoftOAuthForJE(oauth => oauth.Silent());
// ...

Set Microsoft OAuth mode. Instead of oauth => oauth.Interactive(), there are many options you can replace with. See OAuth.

AddMicrosoftOAuthForJE and AddForceMicrosoftOAuthForJE methods add default MicrosoftOAuthClientInfo which Mojang Minecraft launcher uses so that you don't need to pass it everytime you use.

Note that the default Microsoft OAuth is only available on Windows platform. For another platform (Linux, macOS) you need XboxAuthNet.Game.Msal.

// example for XboxAuthNet.Game.Msal
authenticator.AddMsalOAuth(app, msal => msal.Interactive());

3. XboxAuth

authenticator.AddXboxAuthForJE(xbox => xbox.Basic());

// above code is same as
// authenticator.AddXboxAuth(xbox => xbox.WithRelyingParty(JELoginHandler.RelyingParty).Basic());

// another xbox auth method can be:
// 1) authenticator.AddXboxAuthForJE(xbox => xbox.Full());
// 2) authenticator.AddXboxAuthForJE(xbox => xbox.Sisu("<CLIENT-ID>"));
// ...

Set Xbox authentication mode. Instead of xbox => xbox.Basic(), there are many options you can replace with. See XboxAuth.

AddXboxAuthForJE and AddForceXboxAuthForJE methods add default xbox authentication relying party which is used for Minecraft: JE authentication so that you don't need to pass it everytime you use.

4. JEAuthenticator

Set Minecraft: JE authentication mode. See JEAuthenticator.

Last updated