/// <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 */); }
internal Task<LiveLoginResult> RefreshTokenAsync() { var refreshOp = new RefreshTokenOperation( this, this.clientId, this.Session.RefreshToken, this.Session.Scopes, null); return refreshOp.ExecuteAsync(); }