Ejemplo n.º 1
0
        private void ExchangeCodeForToken(string redirectUrl, string authorizationCode)
        {
            Task <LiveLoginResult> task = LiveAuthRequestUtility.ExchangeCodeForTokenAsync(
                this.clientId, this.clientSecret, redirectUrl, authorizationCode);

            task.ContinueWith((tk) =>
            {
                this.OnAuthTaskCompleted(tk.Result);
            });
        }
        private void ExchangeCodeForToken(string authorizationCode)
        {
            Task <LiveLoginResult> task = LiveAuthRequestUtility.ExchangeCodeForTokenAsync(
                this.clientId,
                null,
                LiveAuthUtility.BuildDesktopRedirectUrl(),
                authorizationCode);

            task.ContinueWith((Task <LiveLoginResult> t) =>
            {
                this.OnExchangeCodeCompleted(t.Result);
            });
        }
 private void RefreshToken(Action <LiveLoginResult> completionCallback)
 {
     if (this.refreshTokenInfo != null)
     {
         LiveAuthRequestUtility.RefreshTokenAsync(
             this.clientId,
             null,
             LiveAuthUtility.BuildDesktopRedirectUrl(),
             this.refreshTokenInfo.RefreshToken,
             null     /*scopes*/
             ).ContinueWith(t =>
         {
             this.OnRefreshTokenCompleted(t.Result, completionCallback);
         });
     }
     else
     {
         LiveLoginResult result = new LiveLoginResult(LiveConnectSessionStatus.Unknown, null);
         this.OnRefreshTokenCompleted(result, completionCallback);
     }
 }
Ejemplo n.º 4
0
        private void TryRefreshToken(string redirectUrl)
        {
            Debug.Assert(this.loginStatus != null);

            IEnumerable <string> scopes;
            LiveAuthException    error;
            bool isTokenRequest = this.CheckRefreshTokenRequest(out scopes, out error);

            if (error != null)
            {
                this.OnAuthTaskCompleted(new LiveLoginResult(error));
                return;
            }

            // Try to refresh a token if
            // i) there is a token request or
            // ii) we don't have a token or
            // iii) the current token is expired.
            LiveLoginResult    result  = null;
            LiveConnectSession session = this.loginStatus.Session;
            bool hasValidToken         = session != null && session.IsValid;
            bool shouldRefresh         = (this.refreshTokenHandler != null) && (isTokenRequest || !hasValidToken);

            if (!shouldRefresh)
            {
                this.OnAuthTaskCompleted(null);
                return;
            }

            if (this.initScopes == null)
            {
                // We don't have initScopes, then use the scopes received from Url.
                this.initScopes = scopes;
            }

            this.refreshTokenHandler.RetrieveRefreshTokenAsync().ContinueWith(t =>
            {
                try
                {
                    this.refreshTokenInfo = t.Result;
                    if (this.refreshTokenInfo != null)
                    {
                        string currentUserId = this.publicAuthClient.CurrentUserId;
                        if (currentUserId != null && this.refreshTokenInfo.UserId != currentUserId)
                        {
                            // There is a user Id available in current session. We need to ensure the token provided matches it.
                            result = new LiveLoginResult(new LiveAuthException(
                                                             AuthErrorCodes.InvalidRequest, ErrorText.RefereshTokenNotMatchUserId));
                        }
                        else
                        {
                            LiveAuthRequestUtility.RefreshTokenAsync(
                                this.clientId,
                                this.clientSecret,
                                redirectUrl,
                                this.refreshTokenInfo.RefreshToken,
                                null/*scopes -  We intentially specify null scopes and validate the initScopes after we have the session
                                     * result. With this approach, we can return notConnected if initScopes is not satisfied, and surface
                                     * the error if there is one.
                                     */
                                ).ContinueWith((Task <LiveLoginResult> rt) =>
                            {
                                result = rt.Result;
                                this.OnAuthTaskCompleted(result);
                            });
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    error  = new LiveAuthException(AuthErrorCodes.ClientError, ErrorText.RetrieveRefreshTokenError, ex);
                    result = new LiveLoginResult(error);
                }

                this.OnAuthTaskCompleted(result);
            });
        }