コード例 #1
0
        public void TestExecute()
        {
            WebRequestFactory.Current = new TestWebRequestFactory();

#if NETFX_CORE
            LiveAuthClient authClient = new LiveAuthClient();
#elif WINDOWS_PHONE
            LiveAuthClient authClient = new LiveAuthClient("clientId");
#else
#error This platform needs to be handled.
#endif

            string clientId = "clientId";
            string refreshToken = "refreshToken";
            IEnumerable<string> scopes = new string[]{ "wl.basic" };
            SynchronizationContextWrapper syncContext = SynchronizationContextWrapper.Current;

            var refreshOperation =
                new RefreshTokenOperation(authClient, clientId, refreshToken, scopes, syncContext);
        }
コード例 #2
0
        /// <summary>
        /// Processes authentication result from the server.
        /// Method could return synchronously or asynchronously.
        /// </summary>
        private void ProcessAuthResponse(string responseData, Action<LiveLoginResult> callback)
        {
            if (string.IsNullOrEmpty(responseData))
            {
                // non-connected user scenario. return status unknown.
                callback(new LiveLoginResult(LiveConnectSessionStatus.Unknown, null));
                return;
            }

            Uri responseUrl;
            try
            {
                responseUrl = new Uri(responseData, UriKind.Absolute);
            }
            catch (FormatException)
            {
                callback(new LiveLoginResult(
                    new LiveAuthException(AuthErrorCodes.ServerError, ResourceHelper.GetString("ServerError"))));
                return;
            }

            if (!string.IsNullOrEmpty(responseUrl.Fragment))
            {
                callback(this.ParseResponseFragment(responseUrl.Fragment));
                return;
            }

            if (!string.IsNullOrEmpty(responseUrl.Query))
            {
                IDictionary<string, object> parameters = LiveAuthClient.ParseQueryString(responseUrl.Query);
                if (parameters.ContainsKey(AuthConstants.Code))
                {
                    var authCode = parameters[AuthConstants.Code] as string;
                    if (!string.IsNullOrEmpty(authCode))
                    {
                        var refreshOp = new RefreshTokenOperation(
                            this, 
                            this.clientId,
                            authCode,
                            this.redirectUri,
                            this.syncContext);

                        refreshOp.OperationCompletedCallback = callback;
                        refreshOp.Execute();

                        return;
                    }
                }
                else if (parameters.ContainsKey(AuthConstants.Error))
                {
                    callback(GetErrorResult(parameters));
                    return;
                }
            }

            callback(
                new LiveLoginResult(
                    new LiveAuthException(AuthErrorCodes.ServerError, ResourceHelper.GetString("ServerError"))));
        }
コード例 #3
0
        internal Task<LiveLoginResult> RefreshTokenAsync()
        {
            var refreshOp = new RefreshTokenOperation(
                this,
                this.clientId,
                this.Session.RefreshToken,
                this.Session.Scopes,
                null);

            return refreshOp.ExecuteAsync();
        }
コード例 #4
0
        /// <summary>
        /// Initializes the auth client. Detects if user is already signed in,
        /// If user is already signed in, creates a valid Session.
        /// This call is UI-less.
        /// </summary>
        /// <param name="scopes">The list of offers that the application is requesting user consent for.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public async Task<LiveLoginResult> InitializeAsync(IEnumerable<string> scopes)
        {
            this.PrepareForAsync();

            // Always make a call to the server instead of relying on cached access token for two reasons:
            // 1. user may have revoked the scope
            // 2. user may have previously consented to the scope via a web app, we should not ask again.

            // Use a refresh token if present, if not, use silent flow.
            LiveConnectSession currentSession = this.AuthClient.LoadSession(this);
            this.scopes = (scopes == null) ? new List<string>() : new List<string>(scopes);

            bool hasRefreshToken = currentSession != null && !string.IsNullOrEmpty(currentSession.RefreshToken);
            if (hasRefreshToken)
            {
                var refreshOp = new RefreshTokenOperation(
                    this,
                    this.clientId,
                    currentSession.RefreshToken,
                    this.scopes,
                    null);

                var tcs = new TaskCompletionSource<LiveLoginResult>();

                try
                {
                    LiveLoginResult refreshOpResult = await refreshOp.ExecuteAsync();
                    this.Session = refreshOpResult.Session;
                    this.AuthClient.SaveSession(this.Session);
                    tcs.TrySetResult(refreshOpResult);
                }
                catch (Exception exception)
                {
                    this.Session = null;
                    tcs.TrySetException(exception);
                }
                finally
                {
                    Interlocked.Decrement(ref this.asyncInProgress);
                }

                return await tcs.Task;
            }

            // If we do NOT have a refresh token, use the silent flow.
            return await this.AuthenticateAsync(true /* silent flow */);
        }
コード例 #5
0
        /// <summary>
        /// Retrieve a new access token based on current session information.
        /// </summary>
        internal bool RefreshToken(Action<LiveLoginResult> completionCallback)
        {
            if (this.Session == null || this.Session.IsValid)
            {
                return false;
            }

            var refreshOp = new RefreshTokenOperation(
                this,
                this.clientId,
                this.Session.RefreshToken,
                this.Session.Scopes,
                null)
            {
                OperationCompletedCallback = completionCallback
            };

            refreshOp.Execute();

            return true;
        }