private async void ProcessWebTokenResponse(WebTokenResponse tokenResponse, bool storeAccount = false) { try { string authToken = tokenResponse.Token; // Do something with the token DebugPrint("The token returned : " + authToken); for (int i = 0; i < tokenResponse.Properties.Count; i++) { string msg = string.Format("---ResponseProperties[{0}].key=[{1}], value=[{2}]", i, tokenResponse.Properties.ElementAt(i).Key, tokenResponse.Properties.ElementAt(i).Value); DebugPrint(msg); } WebAccount account = tokenResponse.WebAccount; if (account != null) { DebugPrint("tokenResponse.WebAccount is not null."); string msg = string.Format("---- AccounInfo. Id={0}, UserName={1}", account.Id, account.UserName); DebugPrint(msg); for (int i = 0; i < account.Properties.Count; i++) { string tmpmsg = string.Format("---WebAccount.Properties[{0}].key=[{1}], value=[{2}]", i, account.Properties.ElementAt(i).Key, account.Properties.ElementAt(i).Value); DebugPrint(tmpmsg); } DebugPrint("webaccount.GetPictureAsync"); IRandomAccessStream picStream = await account.GetPictureAsync(WebAccountPictureSize.Size1080x1080); DebugPrint("webaccount.GetPictureAsync successful"); string picStreamSize = string.Format("---WebAccount picture stream size is: {0}", picStream.Size); DebugPrint(picStreamSize); } else { DebugPrint("tokenResponse.WebAccount is null."); } } catch (Exception ex) { DebugPrint("Exeption during ProcessWebTokenResponse: " + ex.Message); } }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public async override Task <IToken> LoginAsync() #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { Logger.Log("Logging in with WebAuthenticationCoreManager..."); string accessToken = string.Empty; #if ENABLE_WINMD_SUPPORT if (BiometricsRequired) { if (await Windows.Security.Credentials.UI.UserConsentVerifier.CheckAvailabilityAsync() == Windows.Security.Credentials.UI.UserConsentVerifierAvailability.Available) { var consentResult = await Windows.Security.Credentials.UI.UserConsentVerifier.RequestVerificationAsync("Please verify your credentials"); if (consentResult != Windows.Security.Credentials.UI.UserConsentVerificationResult.Verified) { Logger.Log("Biometric verification failed."); return(null); } } else { Logger.Log("Biometric verification is not available or not configured."); return(null); } } string userId = Store.GetUserId(UserIdKey); Logger.Log("User Id: " + userId); string URI = string.Format("ms-appx-web://Microsoft.AAD.BrokerPlugIn/{0}", WebAuthenticationBroker.GetCurrentApplicationCallbackUri().Host.ToUpper()); Logger.Log("Redirect URI: " + URI); WebAccountProvider wap = await WebAuthenticationCoreManager.FindAccountProviderAsync("https://login.microsoft.com", Authority); Logger.Log($"Found Web Account Provider for organizations: {wap.DisplayName}"); //var accts = await WebAuthenticationCoreManager.FindAllAccountsAsync(wap); //Logger.Log($"Find All Accounts Status = {accts.Status}"); //if (accts.Status == FindAllWebAccountsStatus.Success) //{ // foreach (var acct in accts.Accounts) // { // Logger.Log($"Account: {acct.UserName} {acct.State.ToString()}"); // } //} //var sap = await WebAuthenticationCoreManager.FindSystemAccountProviderAsync(wap.Id); //if (sap != null) //{ // string displayName = "Not Found"; // if (sap.User != null) // { // displayName = (string)await sap.User.GetPropertyAsync("DisplayName"); // Logger.Log($"Found system account provider {sap.DisplayName} with user {displayName} {sap.User.AuthenticationStatus.ToString()}"); // } //} Logger.Log("Web Account Provider: " + wap.DisplayName); string resource = "https://sts.mixedreality.azure.com"; //https://sts.mixedreality.azure.com/mixedreality.signin WebTokenRequest wtr = new WebTokenRequest(wap, "https://sts.mixedreality.azure.com//.default", ClientId); wtr.Properties.Add("resource", resource); WebAccount account = null; if (!string.IsNullOrEmpty((string)userId)) { account = await WebAuthenticationCoreManager.FindAccountAsync(wap, (string)userId); if (account != null) { Logger.Log("Found account: " + account.UserName); } else { Logger.Log("Account not found"); } } WebTokenRequestResult tokenResponse = null; try { if (account != null) { tokenResponse = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(wtr, account); } else { tokenResponse = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(wtr); //tokenResponse = await WebAuthenticationCoreManager.RequestTokenAsync(wtr); } } catch (Exception ex) { Logger.Log(ex.Message); } Logger.Log("Silent Token Response: " + tokenResponse.ResponseStatus.ToString()); if (tokenResponse.ResponseError != null) { Logger.Log("Error Code: " + tokenResponse.ResponseError.ErrorCode.ToString()); Logger.Log("Error Msg: " + tokenResponse.ResponseError.ErrorMessage.ToString()); foreach (var errProp in tokenResponse.ResponseError.Properties) { Logger.Log($"Error prop: ({errProp.Key}, {errProp.Value})"); } } if (tokenResponse.ResponseStatus == WebTokenRequestStatus.UserInteractionRequired) { WebTokenRequestResult wtrr = null; try { if (account != null) { wtrr = await WebAuthenticationCoreManager.RequestTokenAsync(wtr, account); } else { wtrr = await WebAuthenticationCoreManager.RequestTokenAsync(wtr); } } catch (Exception ex) { Logger.Log(ex.Message); } Logger.Log("Interactive Token Response: " + wtrr.ResponseStatus.ToString()); if (wtrr.ResponseError != null) { Logger.Log("Error Code: " + wtrr.ResponseError.ErrorCode.ToString()); Logger.Log("Error Msg: " + wtrr.ResponseError.ErrorMessage.ToString()); foreach (var errProp in wtrr.ResponseError.Properties) { Logger.Log($"Error prop: ({errProp.Key}, {errProp.Value})"); } } if (wtrr.ResponseStatus == WebTokenRequestStatus.Success) { accessToken = wtrr.ResponseData[0].Token; account = wtrr.ResponseData[0].WebAccount; var properties = wtrr.ResponseData[0].Properties; Username = account.UserName; Logger.Log($"Username = {Username}"); var ras = await account.GetPictureAsync(WebAccountPictureSize.Size64x64); var stream = ras.AsStreamForRead(); var br = new BinaryReader(stream); UserPicture = br.ReadBytes((int)stream.Length); Logger.Log("Access Token: " + accessToken, false); } } if (tokenResponse.ResponseStatus == WebTokenRequestStatus.Success) { foreach (var resp in tokenResponse.ResponseData) { var name = resp.WebAccount.UserName; accessToken = resp.Token; account = resp.WebAccount; Username = account.UserName; Logger.Log($"Username = {Username}"); try { var ras = await account.GetPictureAsync(WebAccountPictureSize.Size64x64); var stream = ras.AsStreamForRead(); var br = new BinaryReader(stream); UserPicture = br.ReadBytes((int)stream.Length); } catch (Exception ex) { Logger.Log($"Exception when reading image {ex.Message}"); } } Logger.Log("Access Token: " + accessToken, false); } if (account != null && !string.IsNullOrEmpty(account.Id)) { Store.SaveUser(UserIdKey, account.Id); } #endif AADToken = accessToken; return(new AADToken(AADToken)); }