/// <summary>
        /// Refreshes access token with the identity provider for the logged in user.
        /// </summary>
        /// <returns>
        /// Task that will complete when the user has finished refreshing access token
        /// </returns>
        public async Task <MobileServiceUser> RefreshUserAsync()
        {
            if (this.CurrentUser == null || string.IsNullOrEmpty(this.CurrentUser.MobileServiceAuthenticationToken))
            {
                throw new InvalidOperationException("MobileServiceUser must be set before calling refresh");
            }
            string response = null;
            MobileServiceHttpClient client = this.HttpClient;

            if (this.AlternateLoginHost != null)
            {
                client = this.AlternateAuthHttpClient;
            }
            try
            {
                response = await client.RequestWithoutHandlersAsync(HttpMethod.Get, RefreshUserAsyncUriFragment, this.CurrentUser, null, MobileServiceFeatures.RefreshToken);
            }
            catch (MobileServiceInvalidOperationException ex)
            {
                string message = string.Empty;
                if (ex.Response != null)
                {
                    switch (ex.Response.StatusCode)
                    {
                    case HttpStatusCode.BadRequest:
                        message = "Refresh failed with a 400 Bad Request error. The identity provider does not support refresh, or the user is not logged in with sufficient permission.";
                        break;

                    case HttpStatusCode.Unauthorized:
                        message = "Refresh failed with a 401 Unauthorized error. Credentials are no longer valid.";
                        break;

                    case HttpStatusCode.Forbidden:
                        message = "Refresh failed with a 403 Forbidden error. The refresh token was revoked or expired.";
                        break;

                    default:
                        message = "Refresh failed due to an unexpected error.";
                        break;
                    }
                    throw new MobileServiceInvalidOperationException(message, innerException: ex, request: ex.Request, response: ex.Response);
                }
                throw;
            }

            if (!string.IsNullOrEmpty(response))
            {
                JToken authToken = JToken.Parse(response);

                this.CurrentUser.MobileServiceAuthenticationToken = (string)authToken["authenticationToken"];
            }

            return(this.CurrentUser);
        }
        public MobileServiceClient(IMobileServiceClientOptions options) : this(options.MobileAppUri, null)
        {
            AlternateLoginHost = options.AlternateLoginHost;
            LoginUriPrefix     = options.LoginUriPrefix;

            var handlers = options.GetDefaultMessageHandlers(this) ?? EmptyHttpMessageHandlers;

            if (handlers.Any())
            {
                HttpClient = new MobileServiceHttpClient(handlers, MobileAppUri, InstallationId, httpRequestTimeout);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the MobileServiceClient class.
        /// </summary>
        /// <param name="applicationUri">
        /// The URI for the Microsoft Azure Mobile Service.
        /// </param>
        /// <param name="applicationKey">
        /// The application key for the Microsoft Azure Mobile Service.
        /// </param>
        /// <param name="handlers">
        /// Chain of <see cref="HttpMessageHandler" /> instances.
        /// All but the last should be <see cref="DelegatingHandler"/>s.
        /// </param>
        public MobileServiceClient(Uri applicationUri, string applicationKey, params HttpMessageHandler[] handlers)
        {
            if (applicationUri == null)
            {
                throw new ArgumentNullException("applicationUri");
            }

            this.ApplicationUri            = applicationUri;
            this.ApplicationKey            = applicationKey;
            this.applicationInstallationId = GetApplicationInstallationId();

            handlers        = handlers ?? EmptyHttpMessageHandlers;
            this.HttpClient = new MobileServiceHttpClient(handlers, this.ApplicationUri, this.applicationInstallationId, this.ApplicationKey);
            this.Serializer = new MobileServiceSerializer();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RemoteTableStorage"/> class.
        /// </summary>
        /// <param name="httpClient">
        /// The <see cref="MobileServiceHttpClient"/> instance to use for the storage context.
        /// </param>
        /// <param name="tableName">
        /// The table name to use with the storage context.
        /// </param>
        public RemoteTableStorage(MobileServiceHttpClient httpClient)
        {
            Debug.Assert(httpClient != null);

            this.httpClient = httpClient;
        }