Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Threading.Tasks;
- using System;
- using System.IO;
- using System.Runtime.Serialization.Json;
- using System.Text;
- using System.Net.Http.Headers;
- using System.Net.Http;
- using Microsoft.Identity.Client;
- using System.Linq;
- using System.Security;
- using System.Threading;
- using UnityEngine.iOS;
- #if UNITY_WSA && !UNITY_EDITOR
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.IO;
- using System.Runtime.Serialization.Json;
- using System.Text;
- using Microsoft.Identity.Client;
- #endif
- public class GraphAuth : MonoBehaviour
- {
- List<string> Scopes;
- private string authority;
- private string ClientId;
- private string tenant;
- private string redirectUrl;
- // Start is called before the first frame update
- async void Start()
- {
- Scopes = new List<string> { "User.Read", "Mail.Read" };
- authority = "https://login.microsoftonline.com/common";
- ClientId = "<myClientId>";
- //string tenant = "consumer";
- //authority = $"https://login.microsoftonline.com/{tenant}";
- redirectUrl = "https://login.microsoftonline.com/common/oauth2/nativeclient";
- await GoGraph();
- }
- // Update is called once per frame
- void Update()
- {
- }
- private async Task<AuthenticationResult> GoGraph()
- {
- Debug.Log("Inside GoGraph!");
- //AuthenticationResult authResult = null;
- Debug.Log($"ClientID={ClientId} and authority:{authority}");
- IPublicClientApplication pca = PublicClientApplicationBuilder
- .Create(ClientId)
- .WithAuthority(authority)
- .WithRedirectUri(redirectUrl)
- .Build();
- var accounts = await pca.GetAccountsAsync();
- //Debug.Log(accounts);
- try {
- return await pca.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
- .ExecuteAsync();
- }
- catch (MsalUiRequiredException ex)
- {
- // No token found in the cache or AAD insists that a form interactive auth is required (e.g. the tenant admin turned on MFA)
- // If you want to provide a more complex user experience, check out ex.Classification
- Debug.Log($"UI Reqd Exception:{ex.Message}");
- return await AcquireByDeviceCodeAsync(pca);
- }
- catch (MsalException msaEx)
- {
- Debug.Log($"Failed to acquire token: {msaEx.Message}");
- return null;
- //FOVFeedback.instance.ModifyText($"Failed to acquire token: {msaEx.Message}");
- }
- catch (Exception ex)
- {
- Debug.Log($"Failed to acquire token: {ex.Message}");
- return null;
- //FOVFeedback.instance.ModifyText($"Failed to acquire token: {ex.Message}");
- }
- Debug.Log("Going out of GoGraph!");
- }
- private async Task<AuthenticationResult> AcquireByDeviceCodeAsync(IPublicClientApplication pca) {
- Debug.Log($"Inside AcquireByDeviceCodeAsync!");
- try
- {
- var result = await pca.AcquireTokenWithDeviceCode(Scopes,
- deviceCodeResult =>
- {
- UnityEngine.WSA.Application.InvokeOnAppThread(() =>
- {
- Debug.Log($"${deviceCodeResult.Message}");
- },true);
- Debug.Log(deviceCodeResult.Message);
- return Task.FromResult(0);
- }).ExecuteAsync();
- Debug.Log(result.Account.Username);
- return result;
- }
- catch (MsalServiceException ex)
- {
- Debug.Log($"MsalServiceException:${ex}");
- return null;
- }
- catch (OperationCanceledException ex)
- {
- Debug.Log($"Operation Canceled Exception:${ex.Message}");
- return null;
- }
- catch (MsalClientException ex)
- {
- Debug.Log($"MsalClientException:{ex.Message}");
- return null;
- }
- catch (Exception ex) {
- Debug.Log($"The last caught exception:{ex.Message}");
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement