public static Credentials WinCred(string url, string usernameFromUrl, SupportedCredentialTypes types) { var creds = new Credential { Target = url, Type = CredentialType.Generic }; if (creds.Load()) { return new UsernamePasswordCredentials { Username = creds.Username, Password = creds.Password } } ; var uri = new Uri(url); var userPart = (string.IsNullOrEmpty(usernameFromUrl) ? string.Empty : usernameFromUrl + "@"); creds.Target = $"git:{uri.Scheme}://{userPart}{uri.Host}"; if (!creds.Load()) { creds.Target += "/"; creds.Load(); } return(new UsernamePasswordCredentials { Username = creds.Username, Password = creds.Password }); }
public bool LoadCredential(out string username, out string password) { _logger.Info("Loading credentials from store..."); using (var credential = new CredentialManagement.Credential()) { credential.Target = RaceControlF1TV; credential.Type = CredentialType.Generic; credential.PersistanceType = PersistanceType.LocalComputer; var loaded = credential.Load(); if (loaded) { username = credential.Username; password = credential.Password; _logger.Info("Credentials loaded from store."); } else { username = null; password = null; _logger.Warn("Credentials not found in store."); } return(loaded); } }
private bool GetFromCredentialStore() { try { Credential credential = new Credential(); credential.Target = ConfigurationManager.AppSettings["CredentialStoreKey"]; credential.Type = CredentialType.Generic; credential.PersistanceType = PersistanceType.Enterprise; if (!credential.Load()) { Utility.LogAction("Failed to load credential from credential manager "); return(false); } _email = credential.Username; _password = credential.Password; return(true); } catch (Exception ex) { Utility.LogAction("Credential manager failed with exception: " + ex.Message); return(false); } }
public System.Net.ICredentials GetCredentials(Uri requestUri) { var cred = new Credential { Target = serverUri.ToString(), PersistanceType = PersistanceType.LocalComputer, Type = CredentialType.Generic }; if (cred.Load()) { return new NetworkCredential(cred.Username, cred.Password); } cred = GetCredFromUser(); if (cred != null) { return new NetworkCredential(cred.Username, cred.Password); } return null; }
/// <summary> /// The get credentials. /// </summary> private void GetCredentials() { using (var cm = new Credential { Target = "VSSonarQubeExtension" }) { if (!cm.Exists()) { return; } cm.Load(); string address = "http://localhost:9000"; try { address = this.configurationHelper.ReadSetting(Context.GlobalPropsId, OwnersId.ApplicationOwnerId, "ServerAddress").Value; } catch (Exception ex) { this.notificationManager.ReportMessage(new Message { Id = "GeneralConfigurationViewModel", Data = "Failed To Connect To Server: " + ex.Message }); this.notificationManager.ReportException(ex); } string bootatstart = "false"; try { bootatstart = this.configurationHelper.ReadSetting(Context.GlobalPropsId, OwnersId.ApplicationOwnerId, GlobalIds.IsConnectAtStartOn).Value; } catch (Exception ex) { this.notificationManager.ReportMessage(new Message { Id = "GeneralConfigurationViewModel", Data = "Failed To Get Connect at Start: " + ex.Message }); } this.UserName = cm.Username; this.Password = ConnectionConfiguration.ConvertToUnsecureString(cm.SecurePassword); this.ServerAddress = address; if (address != null) { AuthtenticationHelper.EstablishAConnection(this.restService, address, cm.Username, ConnectionConfiguration.ConvertToUnsecureString(cm.SecurePassword)); } } }
/// <summary> /// Retrieves a OneDrive Refresh Token from the Windows Credential Manager /// </summary> /// <param name="databaseFilePath">Full local path to the KeePass database for which to retrieve the OneDrive Refresh Token</param> /// <returns>OneDrive Refresh Token if available or NULL if no Refresh Token found for the provided database</returns> public static string GetRefreshTokenFromWindowsCredentialManager(string databaseFilePath) { using (var credential = new Credential {Target = string.Concat("KoenZomers.KeePass.OneDriveSync:", databaseFilePath), Type = CredentialType.Generic}) { credential.Load(); return credential.Exists() ? credential.Password : null; } }
public bool Exists() { CheckNotDisposed(); _unmanagedCodePermission.Demand(); if (string.IsNullOrEmpty(Target)) { throw new InvalidOperationException("Target must be specified to check existance of a credential."); } using (Credential existing = new Credential { Target = Target, Type = Type }) { return existing.Load(); } }
/// <summary> /// The get credentials. /// </summary> private void GetCredentials() { var cm = new Credential { Target = "VSSonarQubeExtension", }; if (!cm.Exists()) { return; } cm.Load(); try { string address = this.configurationHelper.ReadSetting(Context.GlobalPropsId, OwnersId.ApplicationOwnerId, "ServerAddress").Value; string bootatstart = this.configurationHelper.ReadSetting(Context.GlobalPropsId, OwnersId.ApplicationOwnerId, GlobalIds.IsConnectAtStartOn).Value; if (address != null && bootatstart.Equals("true")) { if (AuthtenticationHelper.EstablishAConnection(this.restService, address, cm.Username, ConnectionConfiguration.ConvertToUnsecureString(cm.SecurePassword))) { this.UserName = cm.Username; this.ServerAddress = address; this.Password = ConnectionConfiguration.ConvertToUnsecureString(cm.SecurePassword); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
static void Main(string[] args) { var c = new Credential() { Target = "192.168.20.195", Type = CredentialType.DomainPassword,//windows 凭证 Generic 普通凭证 PersistanceType = PersistanceType.Enterprise,//永久 }; if (c.Exists()) { c.Load(); Console.WriteLine(c.Username); } //GetCredential("192.168.20.30"); //using (PrincipalContext context = new PrincipalContext(ContextType.Machine, "192.168.20.30",null,ContextOptions.Negotiate)) //{ // //if (!currentUser.ToLower().StartsWith("lanxum\\")) // //{ // // Validate(domain, context, new Exception("当前用户未登录域")); // //} // //else // { // try // { // var upUser = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "wcl"); // } // catch (Exception ex) // { // //Validate(domain, context, ex); // } // } //} //var cm = new VistaPrompt(); ////cm.Domain = "lanxum"; //cm.SaveChecked = true; //cm.ShowSaveCheckBox = true; //cm.Title = @"指定已授权的 域(计算机)\用户"; //cm.Message = "123213"; //DialogResult rs = cm.ShowDialog(); ////CredentialManagement(); var us = c.Username.Split('\\'); var httpClient = new HttpClient(new HttpClientHandler() { //UseDefaultCredentials = true Credentials = new System.Net.NetworkCredential(@"transfer218", "Lanxum1234", "WIN-6U432IIN") }); httpClient.GetStringAsync("http://192.168.20.195:8090").ContinueWith(t => { if (t.IsFaulted) { Console.WriteLine(t.Exception.GetBaseException()); } else { Console.WriteLine(t.Result); } }); //var c = new Credential() //{ // Target = "192.168.20.30", // Type = CredentialType.DomainPassword,//windows 凭证 Generic 普通凭证 // PersistanceType = PersistanceType.Enterprise,//永久 // Username = "******", // SecurePassword = cm.SecurePassword, //}; //c.Save(); Console.ReadKey(); }