Esempio n. 1
0
 /// <summary>
 /// Saves an authenticated account to storage.
 /// </summary>
 /// <param name="wi">Object containing the web account info.</param>
 private void SaveWebAccountInfo(WebAccountInfo wi)
 {
     if (wi != null)
     {
         PlatformBase.CurrentCore.Storage.SaveSetting("WAM_" + wi.Type.ToString(), wi, ApplicationData.Current.RoamingSettings, SerializerTypes.Json);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Saves an authenticated account to storage.
 /// </summary>
 /// <param name="wi">Object containing the web account info.</param>
 private void SaveWebAccountInfo(WebAccountInfo wi)
 {
     if(wi != null)
         Platform.Current.Storage.SaveSetting("WAM_" + wi.Type.ToString(), wi, ApplicationData.Current.RoamingSettings, SerializerTypes.Json);
 }
Esempio n. 3
0
        private async void OnWebAccountProviderRequested(WebAccountProviderCommand cmd)
        {
            // Retrieve the provider info instance for the requestd provider
            var pi = Platform.Current.WebAccountManager.GetProviderInfo(cmd.WebAccountProvider.Id);
            if (pi == null)
                throw new ArgumentNullException(nameof(pi));

            try
            {
                WebTokenRequest request = new WebTokenRequest(cmd.WebAccountProvider, pi.Scope, pi.ClientID);

                // Add any properties for this request from the provider info
                if (pi.RequestProperties != null)
                    foreach (var prop in pi.RequestProperties)
                        request.Properties.Add(prop);

                // If the user selected a specific account, RequestTokenAsync will return a token for that account.
                // The user may be prompted for credentials or to authorize using that account with your app
                // If the user selected a provider, the user will be prompted for credentials to login to a new account
                WebTokenRequestResult result = await WebAuthenticationCoreManager.RequestTokenAsync(request);

                if (result.ResponseStatus == WebTokenRequestStatus.Success)
                {
                    var webTokenResponse = result.ResponseData[0];
                    var webAccount = webTokenResponse.WebAccount;

                    // Wrapper the web account data
                    WebAccountInfo wi = new WebAccountInfo();
                    wi.Type = pi.WebAccountType;
                    wi.ProviderID = webAccount.WebAccountProvider.Id;
                    wi.AccountID = !string.IsNullOrEmpty(webAccount.Id) ? webAccount.Id : webAccount.UserName;
                    wi.Authority = webAccount.WebAccountProvider.Authority != null ? webAccount.WebAccountProvider.Authority : "";
                    wi.Token = webTokenResponse.Token;
                    this.SaveWebAccountInfo(wi);

                    Platform.Current.Logger.Log(LogLevels.Information, string.Format("Web Token request successful for AccountID: {0}", wi.AccountID));

                    // Success Callback
                    _successHandler(pi, wi, result);
                }
                else
                {
                    Platform.Current.Logger.Log(LogLevels.Information, "Web Token request error: " + result.ResponseStatus + " Code: " + result.ResponseError.ErrorMessage);

                    // Failed Callback
                    _failedHandler(pi, result);
                }

            }
            catch (Exception ex)
            {
                Platform.Current.Logger.LogError(ex, "Web Token request failed");
                _failedHandler(pi, null);
            }
            finally
            {
                this.Cleanup();
            }
        }
Esempio n. 4
0
        private async void OnWebAccountProviderRequested(WebAccountProviderCommand cmd)
        {
            // Retrieve the provider info instance for the requestd provider
            var pi = this.GetProviderInfo(cmd.WebAccountProvider.Id);

            if (pi == null)
            {
                throw new ArgumentNullException(nameof(pi));
            }

            try
            {
                WebTokenRequest request = new WebTokenRequest(cmd.WebAccountProvider, pi.Scope, pi.ClientID);

                // Add any properties for this request from the provider info
                if (pi.RequestProperties != null)
                {
                    foreach (var prop in pi.RequestProperties)
                    {
                        request.Properties.Add(prop);
                    }
                }

                // If the user selected a specific account, RequestTokenAsync will return a token for that account.
                // The user may be prompted for credentials or to authorize using that account with your app
                // If the user selected a provider, the user will be prompted for credentials to login to a new account
                WebTokenRequestResult result = await WebAuthenticationCoreManager.RequestTokenAsync(request);

                if (result.ResponseStatus == WebTokenRequestStatus.Success)
                {
                    var webTokenResponse = result.ResponseData[0];
                    var webAccount       = webTokenResponse.WebAccount;

                    // Wrapper the web account data
                    WebAccountInfo wi = new WebAccountInfo();
                    wi.Type       = pi.WebAccountType;
                    wi.ProviderID = webAccount.WebAccountProvider.Id;
                    wi.AccountID  = !string.IsNullOrEmpty(webAccount.Id) ? webAccount.Id : webAccount.UserName;
                    wi.Authority  = webAccount.WebAccountProvider.Authority != null ? webAccount.WebAccountProvider.Authority : "";
                    wi.Token      = webTokenResponse.Token;
                    this.SaveWebAccountInfo(wi);

                    PlatformBase.CurrentCore.Logger.Log(LogLevels.Information, string.Format("Web Token request successful for AccountID: {0}", wi.AccountID));

                    // Success Callback
                    _successHandler(pi, wi, result);
                }
                else
                {
                    PlatformBase.CurrentCore.Logger.Log(LogLevels.Information, "Web Token request error: " + result.ResponseStatus + " Code: " + result.ResponseError.ErrorMessage);

                    // Failed Callback
                    _failedHandler(pi, result);
                }
            }
            catch (Exception ex)
            {
                PlatformBase.CurrentCore.Logger.LogError(ex, "Web Token request failed");
                _failedHandler(pi, null);
            }
            finally
            {
                this.Cleanup();
            }
        }