Esempio n. 1
0
        public async void AuthenticateWithRequestToken(
            String WebAccountProviderID,
            String Authority,
            String Scope,
            String ClientID,
            String TokenBindingTarget,
            String RequestProperties)
        {
            try
            {
                //
                //create webTokenRequest with ProviderID, Scope, ClientId
                //
                DebugPrint("creating the webAccountProviderID");

                WebAccountProvider provider = null;
                if (String.IsNullOrEmpty(Authority))
                {
                    DebugPrint("calling FindAccountProviderAsync...");
                    provider = await WebAuthenticationCoreManager.FindAccountProviderAsync(WebAccountProviderID);
                }
                else
                {
                    DebugPrint("calling FindAccountProviderWithAuthorityAsync...");
                    provider = await WebAuthenticationCoreManager.FindAccountProviderAsync(
                        WebAccountProviderID,
                        Authority);
                }

                DebugPrint("Provider Id: " + provider.Id);
                DebugPrint("Provider DisplayName: " + provider.DisplayName);

                WebTokenRequestPromptType prompt = ForceUiCheckBox.IsChecked.Value ?
                                                   WebTokenRequestPromptType.ForceAuthentication : WebTokenRequestPromptType.Default;

                WebTokenRequest webTokenRequest = new WebTokenRequest(
                    provider,
                    Scope,
                    ClientID,
                    prompt);

                //
                // add properties to the tokenrequest if the IDP plugin requires
                //

                AddRequestProperties(RequestProperties, webTokenRequest);

                DebugPrint("Call RequestTokenAsync");
                WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager.RequestTokenAsync(webTokenRequest);

                HandleResult(webTokenRequestResult);
            }
            catch (Exception ex)
            {
                DebugPrint("RequestToken failed: " + ex.Message);
            }
        }
Esempio n. 2
0
        private async void AuthenticateWithGetTokenSilently(
            String WebAccountProviderID,
            String authority,
            String Scope,
            String ClientID,
            String TokenBindingTarget,
            String RequestProperties)
        {
            try
            {
                //
                //create webTokenRequest with ProviderID, Scope, ClientId
                //
                DebugPrint("creating the webAccountProviderID");
                WebAccountProvider provider = null;
                if (String.IsNullOrEmpty(authority))
                {
                    DebugPrint("calling FindAccountProviderAsync...");
                    provider = await WebAuthenticationCoreManager.FindAccountProviderAsync(WebAccountProviderID);
                }
                else
                {
                    DebugPrint("calling FindAccountProviderWithAuthorityAsync...");
                    provider = await WebAuthenticationCoreManager.FindAccountProviderAsync(
                        WebAccountProviderID,
                        Authority.Text);
                }

                WebTokenRequestPromptType tokenType = WebTokenRequestPromptType.Default;

                if (ForceUiCheckBox.IsChecked.Value)
                {
                    tokenType = WebTokenRequestPromptType.ForceAuthentication;
                }

                WebTokenRequest webTokenRequest = new WebTokenRequest(
                    provider,
                    Scope,
                    ClientID, tokenType);

                //
                //adding properties to the tokenrequest if the IDP plugin requires
                DebugPrint("Adding properties to TokenRequest");
                webTokenRequest.Properties.Add("facebook_properties", "additional_properties");

                AddRequestProperties(RequestProperties, webTokenRequest);

                DebugPrint("Call GetTokenSilently");
                WebTokenRequestResult result;
                bool       useAccount = UseAccount.IsChecked.Value;
                WebAccount account    = AccountsComboBox.SelectedItem as WebAccount;
                if (useAccount && account != null)
                {
                    result = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest, account);
                }
                else
                {
                    result = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest);
                }

                HandleResult(result);
            }
            catch (Exception ex)
            {
                DebugPrint("GetTokenSilently failed: " + ex.Message);
            }
        }
        private async Task <bool> RetrieveToken(bool showUi)
        {
            ClearResults();
            try
            {
                var provider = await WebAuthenticationCoreManager.FindAccountProviderAsync(WebProviderId);

                if (provider == null)
                {
                    UpdateResults($"Provider for {WebProviderId} is not installed. Please ensure it has been deployed");
                    return(false);
                }

                WebTokenRequestPromptType tokenType       = WebTokenRequestPromptType.Default;
                WebTokenRequest           webTokenRequest = new WebTokenRequest(
                    provider,
                    "all",
                    "", tokenType);

                webTokenRequest.Properties.Add("UI", "Simplest");

                WebTokenRequestResult webTokenRequestResult = null;
                if (showUi)
                {
                    webTokenRequestResult = await WebAuthenticationCoreManager.RequestTokenAsync(webTokenRequest);
                }
                else
                {
                    webTokenRequestResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest);
                }


                switch (webTokenRequestResult.ResponseStatus)
                {
                case WebTokenRequestStatus.Success:
                    UpdateResults("Web Token retrieved successfully\n");
                    int count = 0;
                    foreach (var result in webTokenRequestResult.ResponseData)
                    {
                        UpdateResults($"Token {count++} = {result.Token} \n");
                    }
                    break;

                case WebTokenRequestStatus.UserCancel:
                    // Handle user cancel by resuming pre-login screen
                    UpdateResults("User cancelled the authentication");
                    break;

                case WebTokenRequestStatus.AccountProviderNotAvailable:

                    // fall back to WebAuthenticationBroker
                    UpdateResults("WebTokenRequestStatus.AccountProviderNotAvailable");
                    break;

                case WebTokenRequestStatus.ProviderError:
                    UpdateResults(string.Format("Error: 0x{0:X08} message: {1}", webTokenRequestResult.ResponseError.ErrorCode, webTokenRequestResult.ResponseError.ErrorMessage));
                    break;

                case WebTokenRequestStatus.UserInteractionRequired:
                    UpdateResults("WebTokenRequestStatus.UserInteractionRequired");

                    if (webTokenRequestResult.ResponseError != null)
                    {
                        UpdateResults(string.Format("Error: 0x{0:X08} message: {1}", webTokenRequestResult.ResponseError.ErrorCode, webTokenRequestResult.ResponseError.ErrorMessage));
                    }
                    break;

                default:
                    UpdateResults("Unhandled webTokenRequestResult.ResponseStatus: " + webTokenRequestResult.ResponseStatus);
                    break;
                }
            } catch (Exception ex)
            {
            }
            return(false);
        }