/// <summary> /// Worker method to acquire, refresh a token /// this method will show a browser as needed to /// complete the authentication flow /// </summary> /// <param name="browser"></param> /// <returns></returns> public Task <string> AcquireToken(WebBrowser browser) { _Browser = browser; _TCS = new TaskCompletionSource <string>(); //Check if existing token has expired, if so try refresh if (_AuthContext.IsAccessTokenExpired()) { //If expired and we have a refresh token, try that if (_AuthContext.IsRefreshViable()) { GetRefreshToken(); } else { //Start the flow from the top with the user providing credentials NavigateBrowserToAuthScreen(); } } else { _TCS.SetResult(_AuthContext.AccessToken); } return(_TCS.Task); }