using System.Collections.Generic;
using UnityEngine;
using Microsoft.Identity.Client;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using System;
using System.Threading.Tasks;
public class MSALLoginScript : MonoBehaviour
{
public GameObject webRequestObj;
public MyWebRequest webRequest;
public string LoginUsername;
public string LoginPassword;
private const string clientId = "4c203cba-62cd-42e4-984d-d4b765c65051";
private const string authority = "https://login.microsoftonline.com/organizations/v2.0";
public const string redirectUri = "http://localhost:3011/";
private string[] scopes = { "User.Read" };
private IPublicClientApplication publicClientApp;
private AuthenticationResult authenticationResult;
// Public property to store the user's email
public string UserEmail;
public bool LoggedIn;
private void Start()
{
InitializeMSAL();
LoggedIn = false;
}
void InitializeMSAL()
{
Debug.LogError("MSAL INITIALIZED");
publicClientApp = PublicClientApplicationBuilder
.Create(clientId)
.WithAuthority(new System.Uri(authority))
.WithRedirectUri(redirectUri)
.Build();
}
public void RequestLogin()
{
Debug.LogError("TRYING TO LAUNCH LOGIN PAGE");
List<string> scopeList = new List<string>(scopes);
// Define your custom state value here
string customState = "YourCustomStateValue";
Dictionary<string, string> extraQueryParams = new Dictionary<string, string>
{
{ "state", customState }
};
publicClientApp.AcquireTokenInteractive(scopeList)
.WithUseEmbeddedWebView(true)
.WithPrompt(Prompt.SelectAccount)
.ExecuteAsync()
.ContinueWith(task =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.LogError($"MSAL Login failed: {task.Exception.Message}");
}
else
{
authenticationResult = task.Result;
// Retrieve user's email from the account property
Debug.Log($"MSAL Login successful. Access Token: {authenticationResult.AccessToken}, User Email: {UserEmail}");
UserEmail = authenticationResult.Account.Username;
StaticValues.Email = UserEmail;
webRequest.Set_SentBy(UserEmail);
LoggedIn = true;
}
});
Debug.LogError("CODE SHOULD'VE RAN");
}
private async Task RequestLoginWithCustomState(string customState)
{
List<string> scopeList = new List<string>(scopes);
Dictionary<string, string> extraQueryParams = new Dictionary<string, string>
{
{ "state", customState }
};
await publicClientApp.AcquireTokenInteractive(scopeList)
.WithPrompt(Prompt.SelectAccount)
.WithExtraQueryParameters(extraQueryParams)
.ExecuteAsync();
}
public void RequestManualLogin()
{
Debug.LogError("TRYING TO MANUALLY LOG IN");
List<string> scopeList = new List<string>(scopes);
publicClientApp.AcquireTokenByUsernamePassword(scopeList, LoginUsername, LoginPassword)
.ExecuteAsync()
.ContinueWith(task =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.LogError($"MSAL Login failed: {task.Exception.Message}");
}
else
{
authenticationResult = task.Result;
// Retrieve user's email from the account property
Debug.Log($"MSAL Login successful. Access Token: {authenticationResult.AccessToken}, User Email: {UserEmail}");
UserEmail = authenticationResult.Account.Username;
StaticValues.Email = UserEmail;
webRequest.Set_SentBy(UserEmail);
LoggedIn = true;
}
});
Debug.LogError("CODE SHOULDVE RAN");
}
public void SetLoginUsername(string input)
{
LoginUsername = input;
}
public void SetLoginPassword(string input)
{
LoginPassword = input;
}
}
}
Good afternoon peeps, I've been busy with an app for a company. They want a microsoft login in their app, and want the app to be reachable by a lot of devices.
So, I thought, I will make a WebGL build.
I use Microsoft.Identity.Client to open the browser with a login tab, and then get the email after the login succeeded.
It all works fine in a windows build, but in a WebGL build just nothing happens. I tried adding error debug statements to view in a development WebGL build, and they all execute fine, which means the Microsoft code also runs.
I've also tried to use the .AcquireTokenByUsernamePassword function, which also executes I think, but does nothing.
Anybody got an idea how to fix this? I've been trying for two weeks maybe.
Thanks in advance!
I've tried:
- Switching to .NET Framework
- Using .WithUseEmbeddedView(true)
- Assembling the URL to open (Is of no use, because the login isn't even initialized)
- Using .AcquireTokenByUsernamePassword