public async Task <bool> DoSignInAsync(string description = null, bool showCancelButton = true)
        {
            // Return if valid WDP credentials are available
            if (await DevicePortalUtil.IsSignedInAsync())
            {
                return(true);
            }

            // Set the size of the popup box
            ViewboxElement.Width  = Math.Min(0.75 * WindowWidth, ViewboxMaxWidth);
            ViewboxElement.Height = 0.5 * ViewboxElement.Width;

            // Reset and customize the WDP control
            WdpLoginControl.Description      = description;
            WdpLoginControl.ShowCancelButton = showCancelButton;
            WdpLoginControl.Reset();

            // Set up a TaskCompletionSource to handle the event and complete the function
            var tcs = new TaskCompletionSource <bool>();
            TypedEventHandler <object, SignInCompletedArgs> signInCompletedHandler = null;

            signInCompletedHandler = (s, e) =>
            {
                if (e.IsUserInitiated || e.IsSuccessful)
                {
                    tcs.TrySetResult(e.IsSuccessful);
                }
            };
            WdpLoginControl.SignInCompleted += signInCompletedHandler;

            // Set position of the popup
            PopupElement.HorizontalOffset = (WindowWidth / 2) - (ViewboxElement.Width / 2);
            PopupElement.VerticalOffset   = (WindowHeight / 2) - (ViewboxElement.Height / 2);

            // Show popup
            PopupElement.IsOpen = true;

            // Wait for the result
            var result = await tcs.Task;

            PopupElement.IsOpen = false;

            // Remove event handlers
            WdpLoginControl.SignInCompleted -= signInCompletedHandler;

            return(result);
        }
Exemple #2
0
        public async Task <bool> SignInAsync(bool isUserInitiated = false)
        {
            await _semaphoreSlim.WaitAsync();

            try
            {
                // Don't try signing in again if we're already signed in
                if (_isSignedIn)
                {
                    return(true);
                }

                IsInputEnabled = false;

                _isSignedIn = await DevicePortalUtil.IsSignedInAsync();

                if (!_isSignedIn)
                {
                    _isSignedIn = await DevicePortalUtil.SignInAsync(Username, Password);
                }

                _finalSignInEventFired = _isSignedIn;

                SignInCompleted?.Invoke(this, new SignInCompletedArgs
                {
                    IsSuccessful    = _isSignedIn,
                    IsUserInitiated = isUserInitiated,
                });

                IsTransitionVisible = false;
                IsVisible           = IsSignInVisible = IsInputEnabled = !_isSignedIn;

                return(_isSignedIn);
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
        private async Task <bool> UpdatePrivacyLevelAsync(int level, bool promptUser = true)
        {
            // Return false if we're not signed in and we're not allowed to prompt the user
            if (!promptUser && !await DevicePortalUtil.IsSignedInAsync())
            {
                return(false);
            }

            // Prompt the user for credentials if not logged in
            if (await LoginPopupControl.SignInAsync(Common.GetLocalizedText("PrivacySignInDescription")))
            {
                var cred = DevicePortalUtil.GetCredential();
                if (cred != null)
                {
                    if (await DevicePortalUtil.SetTelemetryLevelAsync(cred.UserName, cred.Password, level))
                    {
                        _previousLevel = level;
                        return(true);
                    }
                }
            }

            return(false);
        }