JELoginHandler¶
Creating JELoginHandler instance¶
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.
Microsoft WebView2 Requirements
This method uses Microsoft WebView2 for displaying Microsoft OAuth login page. You must know that:
- Microsoft WebView2 is only available on Windows. For another platform, see Authentication with MSAL.
- To run WebView2, The users (including developer and end user) must have the WebView2 Runtime installed. See this document to distribute your launcher with WebView2. (For example, you can automate runtime installation with direct download link: https://go.microsoft.com/fwlink/p/?LinkId=2124703)
If you don't want to use WebView2, see Authentication with MSAL.
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 identify 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¶
Browser Cache
Signout
method does not clear WebView2 browser cache. For clearing it, call SignoutWithBrowser
instead.
Signing out from the Selected Account¶
Browser Cache
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¶
Initialize Authenticator
instance with the specific account to login. Another ways to initialize this:
Initialize Authenticator
with new empty account.
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.
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.