private async Task YouTrackClientLogin(Result <ConnectionData> result) { if (result.IsSuccess && result.Data.IsLoggedIn) { try { var cred = new YouTrackCredentials() { Login = result.Data.UserName, Password = result.Data.Password, Host = result.Data.Host }; await _youTrackClient.LoginAsync(cred); } catch (Exception ex) { _logger.Warn("Couldn't login user using stored credentials. Credentials must have been changed or there is no internet connection", ex); } } }
public async Task LoginAsync(YouTrackCredentials youTrackCredentials) { if (IsConnected) { return; } OnConnectionChanged(new ConnectionData() { IsLoggingIn = true }); if (string.IsNullOrEmpty(youTrackCredentials.Login) || string.IsNullOrEmpty(youTrackCredentials.Password)) { throw new Exception("Credentials fields cannot be empty"); } var connectionData = ConnectionData.NotLogged; try { connectionData = new ConnectionData() { Password = youTrackCredentials.Password, Host = youTrackCredentials.Host, IsLoggingIn = false }; _youTrackClient = await CreateYouTrackClient(youTrackCredentials); connectionData.UserName = _youTrackClient.ApiConnection.Credentials.Login; connectionData.IsLoggedIn = true; } finally { OnConnectionChanged(connectionData); } }
private async Task <IYouTrackClient> CreateYouTrackClientAsync(YouTrackCredentials youTrackCredentials) { _logger.Info($"Calling CreateYouTrackClient. Host: {youTrackCredentials.Host}"); var credentials = new Credentials(youTrackCredentials.Login, youTrackCredentials.Password); var apiUrl = youTrackCredentials.Host.AbsoluteUri; if (!apiUrl.EndsWith("/")) { apiUrl += "/rest"; } else { apiUrl += "rest"; } var apiConnection = new Connection(youTrackCredentials.Host, new Uri(apiUrl), credentials); var client = new EnterpriseYouTrackClient(apiConnection); var user = await client.UserClient.Login(); //will throw exception if not authenticated // await client.UserClient.GetCurrentUserInfo(); return(client); }
public YouTrackClient(YouTrackCredentials credentials) { this.credentials = credentials; if (this.credentials.PermanentToken?.Length > 0) { this.client = new HttpClient(); this.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AH.Unprotect(this.credentials.PermanentToken)); this.reauthenticate = this.ReauthenticateErrorAsync; } else if (!string.IsNullOrEmpty(this.credentials.UserName)) { this.client = new HttpClient(new HttpClientHandler() { CookieContainer = new CookieContainer() }, true); this.reauthenticate = this.ReauthenticateUserNamePasswordAsync; this.needFirstAuth = true; } else { this.client = new HttpClient(); this.reauthenticate = this.ReauthenticateErrorAsync; } }