Advertisement
Guest User

Untitled

a guest
Jul 20th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Threading.Tasks;
  5. using System;
  6. using System.IO;
  7. using System.Runtime.Serialization.Json;
  8. using System.Text;
  9. using System.Net.Http.Headers;
  10. using System.Net.Http;
  11. using Microsoft.Identity.Client;
  12. using System.Linq;
  13. using System.Security;
  14. using System.Threading;
  15. using UnityEngine.iOS;
  16. #if UNITY_WSA && !UNITY_EDITOR
  17. using System.Net.Http;
  18. using System.Net.Http.Headers;
  19. using System.IO;
  20. using System.Runtime.Serialization.Json;
  21. using System.Text;
  22. using Microsoft.Identity.Client;
  23. #endif
  24.  
  25. public class GraphAuth : MonoBehaviour
  26. {
  27.     List<string> Scopes;
  28.     private string authority;
  29.     private string ClientId;
  30.     private string tenant;
  31.     private string redirectUrl;
  32.  
  33.  
  34.     // Start is called before the first frame update
  35.     async void Start()
  36.     {
  37.         Scopes = new List<string> { "User.Read", "Mail.Read" };
  38.         authority = "https://login.microsoftonline.com/common";                    
  39.         ClientId = "<myClientId>";
  40.         //string tenant = "consumer";
  41.         //authority = $"https://login.microsoftonline.com/{tenant}";
  42.         redirectUrl = "https://login.microsoftonline.com/common/oauth2/nativeclient";
  43.  
  44.         await GoGraph();
  45.     }
  46.  
  47.     // Update is called once per frame
  48.     void Update()
  49.     {
  50.        
  51.     }
  52.  
  53.     private async Task<AuthenticationResult> GoGraph()
  54.     {
  55.  
  56.         Debug.Log("Inside GoGraph!");                        
  57.         //AuthenticationResult authResult = null;
  58.         Debug.Log($"ClientID={ClientId} and authority:{authority}");
  59.             IPublicClientApplication pca = PublicClientApplicationBuilder
  60.             .Create(ClientId)
  61.             .WithAuthority(authority)            
  62.             .WithRedirectUri(redirectUrl)
  63.             .Build();
  64.            
  65.             var accounts = await pca.GetAccountsAsync();
  66.             //Debug.Log(accounts);
  67.            
  68.         try {
  69.             return await pca.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
  70.                   .ExecuteAsync();                        
  71.         }
  72.         catch (MsalUiRequiredException ex)
  73.         {
  74.             // No token found in the cache or AAD insists that a form interactive auth is required (e.g. the tenant admin turned on MFA)
  75.             // If you want to provide a more complex user experience, check out ex.Classification
  76.             Debug.Log($"UI Reqd Exception:{ex.Message}");
  77.             return await AcquireByDeviceCodeAsync(pca);
  78.         }        
  79.         catch (MsalException msaEx)
  80.         {
  81.             Debug.Log($"Failed to acquire token: {msaEx.Message}");
  82.             return null;
  83.             //FOVFeedback.instance.ModifyText($"Failed to acquire token: {msaEx.Message}");
  84.         }
  85.         catch (Exception ex)
  86.         {
  87.             Debug.Log($"Failed to acquire token: {ex.Message}");
  88.             return null;
  89.             //FOVFeedback.instance.ModifyText($"Failed to acquire token: {ex.Message}");
  90.         }
  91.        
  92.         Debug.Log("Going out of GoGraph!");
  93.     }
  94.  
  95.     private async Task<AuthenticationResult> AcquireByDeviceCodeAsync(IPublicClientApplication pca) {
  96.         Debug.Log($"Inside AcquireByDeviceCodeAsync!");
  97.         try
  98.         {
  99.            
  100.             var result = await pca.AcquireTokenWithDeviceCode(Scopes,
  101.                 deviceCodeResult =>
  102.                 {
  103.                     UnityEngine.WSA.Application.InvokeOnAppThread(() =>
  104.                     {
  105.                         Debug.Log($"${deviceCodeResult.Message}");
  106.                     },true);
  107.                     Debug.Log(deviceCodeResult.Message);
  108.                     return Task.FromResult(0);                                                                            
  109.                 }).ExecuteAsync();
  110.                 Debug.Log(result.Account.Username);
  111.                 return result;                                                
  112.         }
  113.         catch (MsalServiceException ex)
  114.         {
  115.             Debug.Log($"MsalServiceException:${ex}");
  116.             return null;
  117.         }
  118.         catch (OperationCanceledException ex)
  119.         {
  120.             Debug.Log($"Operation Canceled Exception:${ex.Message}");
  121.             return null;
  122.         }
  123.         catch (MsalClientException ex)
  124.         {
  125.             Debug.Log($"MsalClientException:{ex.Message}");
  126.             return null;
  127.         }
  128.         catch (Exception ex) {
  129.             Debug.Log($"The last caught exception:{ex.Message}");
  130.             return null;
  131.         }
  132.        
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement