/// <summary> /// Add Connection driver to use Windows Remote management /// </summary> /// <param name="optionsBuilder">WmiContextOptionsBuilder to extend</param> /// <param name="computerName">Computer name or IP address to connect</param> /// <param name="nameSpace">WMI Namespace to work with</param> /// <param name="authenticationMechanism">Authentication Mechanism</param> /// <param name="domain">User domain</param> /// <param name="userName">User name</param> /// <param name="password">User password</param> /// <returns></returns> public static WmiContextOptionsBuilder UseCim( this WmiContextOptionsBuilder optionsBuilder, string computerName, string nameSpace, PasswordAuthenticationMechanism authenticationMechanism, string domain, string userName, string password) { optionsBuilder.Options.Connection = new CimConnection( computerName, nameSpace, authenticationMechanism, domain, userName, password); return(optionsBuilder); }
public DnsProviderMSDNS(Dictionary <string, string> credentials, Dictionary <string, string> parameters) { _server = parameters.ContainsKey("dnsservername") ? parameters["dnsservername"] : Environment.MachineName; _serverip = parameters.ContainsKey("ipaddress") ? parameters["ipaddress"] : null; _serverConnectionName = string.IsNullOrEmpty(_serverip) ? _server : _serverip; _username = credentials.ContainsKey("username") ? credentials["username"] : null; if (credentials.ContainsKey("password")) { _password = new SecureString(); credentials["password"].ToList().ForEach(o => _password.AppendChar(o)); _password.MakeReadOnly(); } else { _password = null; } _domain = credentials.ContainsKey("domain") ? credentials["domain"] : null; _customPropagationDelay = parameters.ContainsKey("propagationdelay") ? Convert.ToInt32(parameters["propagationdelay"]) : (int?)null; if (credentials.ContainsKey("protocol") && credentials["protocol"].ToLowerInvariant() == "winrm") { _protocol = WindowsRemotingProtocol.WinRM; } if (credentials.ContainsKey("authentication")) { switch (credentials["authentication"].ToLowerInvariant()) { case "basic": _authMechanism = PasswordAuthenticationMechanism.Basic; break; case "credssp": _authMechanism = PasswordAuthenticationMechanism.CredSsp; break; case "default": _authMechanism = PasswordAuthenticationMechanism.Default; break; case "digest": _authMechanism = PasswordAuthenticationMechanism.Digest; break; case "kerberos": _authMechanism = PasswordAuthenticationMechanism.Kerberos; break; case "negotiate": _authMechanism = PasswordAuthenticationMechanism.Negotiate; break; default: _authMechanism = PasswordAuthenticationMechanism.NtlmDomain; break; } } }
public WSManHelper(string ComputerName, PasswordAuthenticationMechanism AuthType, string Domain, string UserName, string Password, uint Port = 5985) { SecureString securepassword = new SecureString(); foreach (char c in Password) { securepassword.AppendChar(c); } ClassSetup(ComputerName, AuthType, Domain, UserName, securepassword, Port); }
internal void ThrowInvalidAuthenticationTypeError(string operationName, string parameterName, PasswordAuthenticationMechanism authentication) { object[] objArray = new object[5]; objArray[0] = authentication; objArray[1] = ImpersonatedAuthenticationMechanism.None; objArray[2] = ImpersonatedAuthenticationMechanism.Negotiate; objArray[3] = ImpersonatedAuthenticationMechanism.Kerberos; objArray[4] = ImpersonatedAuthenticationMechanism.NtlmDomain; string str = string.Format(CultureInfo.CurrentUICulture, Strings.InvalidAuthenticationTypeWithNullCredential, objArray); PSArgumentOutOfRangeException pSArgumentOutOfRangeException = new PSArgumentOutOfRangeException(parameterName, (object)authentication, str); this.ThrowTerminatingError(pSArgumentOutOfRangeException, operationName); }
private void ClassSetup(string ComputerName, PasswordAuthenticationMechanism AuthType, string Domain, string UserName, SecureString Password, uint Port) { if (AuthType == PasswordAuthenticationMechanism.Default) { AuthType = PasswordAuthenticationMechanism.Kerberos; } Credentials = new CimCredential(AuthType, Domain, UserName, Password); SessionOptions = new WSManSessionOptions() { DestinationPort = Port }; SessionOptions.AddDestinationCredentials(Credentials); SystemSession = CimSession.Create(ComputerName, SessionOptions); Connected = SystemSession.TestConnection(out CimInstance TmpInstance, out CimException TmpExeption); }
/// <summary> /// <para> /// Throw invalid AuthenticationType /// </para> /// </summary> /// <param name="operationName"></param> /// <param name="parameterName"></param> /// <param name="authentication"></param> internal void ThrowInvalidAuthenticationTypeError( string operationName, string parameterName, PasswordAuthenticationMechanism authentication) { string message = String.Format(CultureInfo.CurrentUICulture, Strings.InvalidAuthenticationTypeWithNullCredential, authentication, ImpersonatedAuthenticationMechanism.None, ImpersonatedAuthenticationMechanism.Negotiate, ImpersonatedAuthenticationMechanism.Kerberos, ImpersonatedAuthenticationMechanism.NtlmDomain); PSArgumentOutOfRangeException exception = new PSArgumentOutOfRangeException( parameterName, authentication, message); ThrowTerminatingError(exception, operationName); }
/// <summary> /// Create credentials based on given authentication type and PSCredential. /// </summary> /// <param name="psCredentials"></param> /// <param name="passwordAuthentication"></param> /// <returns></returns> internal CimCredential CreateCimCredentials(PSCredential psCredentials, PasswordAuthenticationMechanism passwordAuthentication, string operationName, string parameterName) { DebugHelper.WriteLogEx("PSCredential:{0}; PasswordAuthenticationMechanism:{1}; operationName:{2}; parameterName:{3}.", 0, psCredentials, passwordAuthentication, operationName, parameterName); CimCredential credentials = null; if (psCredentials != null) { NetworkCredential networkCredential = psCredentials.GetNetworkCredential(); DebugHelper.WriteLog("Domain:{0}; UserName:{1}; Password:{2}.", 1, networkCredential.Domain, networkCredential.UserName, psCredentials.Password); credentials = new CimCredential(passwordAuthentication, networkCredential.Domain, networkCredential.UserName, psCredentials.Password); } else { ImpersonatedAuthenticationMechanism impersonatedAuthentication; switch (passwordAuthentication) { case PasswordAuthenticationMechanism.Default: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.None; break; case PasswordAuthenticationMechanism.Negotiate: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.Negotiate; break; case PasswordAuthenticationMechanism.Kerberos: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.Kerberos; break; case PasswordAuthenticationMechanism.NtlmDomain: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.NtlmDomain; break; default: ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return(null); } credentials = new CimCredential(impersonatedAuthentication); } DebugHelper.WriteLogEx("return credential {0}", 1, credentials); return(credentials); }
/// <summary> /// Constructor that configures connection to host using specified credentials and takes WMI namespace /// </summary> /// <param name="computerName">Computer name or IP address to connect</param> /// <param name="nameSpace">Namespace to configure</param> /// <param name="authenticationMechanism">Authentication mechanism to be used</param> /// <param name="domain">User domain</param> /// <param name="userName">User name</param> /// <param name="password">User password</param> public CimConnection(string computerName, string nameSpace, PasswordAuthenticationMechanism authenticationMechanism, string domain, string userName, string password) { var securePassword = new SecureString(); foreach (var c in password) { securePassword.AppendChar(c); } var credential = new CimCredential(authenticationMechanism, domain, userName, securePassword); var sessionOptions = new WSManSessionOptions(); sessionOptions.AddDestinationCredentials(credential); _connection = CimSession.Create(computerName, sessionOptions); _nameSpace = nameSpace; }
/// <summary> /// Creates a new Credentials /// </summary> public CimCredential(PasswordAuthenticationMechanism authenticationMechanism, string domain, string userName, SecureString password) { if (userName == null) { throw new ArgumentNullException("userName"); } string strAuthenticationMechanism = null; if (authenticationMechanism == PasswordAuthenticationMechanism.Default) { strAuthenticationMechanism = MI_AuthType.DEFAULT; } else if (authenticationMechanism == PasswordAuthenticationMechanism.Basic) { strAuthenticationMechanism = MI_AuthType.BASIC; } else if (authenticationMechanism == PasswordAuthenticationMechanism.Digest) { strAuthenticationMechanism = MI_AuthType.DIGEST; } else if (authenticationMechanism == PasswordAuthenticationMechanism.Negotiate) { strAuthenticationMechanism = MI_AuthType.NEGO_WITH_CREDS; } else if (authenticationMechanism == PasswordAuthenticationMechanism.Kerberos) { strAuthenticationMechanism = MI_AuthType.KERBEROS; } else if (authenticationMechanism == PasswordAuthenticationMechanism.NtlmDomain) { strAuthenticationMechanism = MI_AuthType.NTLM; } else if (authenticationMechanism == PasswordAuthenticationMechanism.CredSsp) { strAuthenticationMechanism = MI_AuthType.CREDSSP; } else { throw new ArgumentOutOfRangeException("authenticationMechanism"); } NativeCimCredential.CreateCimCredential(strAuthenticationMechanism, domain, userName, password, out credential); }
/// <summary> /// Create credentials based on given authentication type and PSCredential /// </summary> /// <param name="psCredentials"></param> /// <param name="passwordAuthentication"></param> /// <returns></returns> internal CimCredential CreateCimCredentials(PSCredential psCredentials, PasswordAuthenticationMechanism passwordAuthentication, string operationName, string parameterName) { DebugHelper.WriteLogEx("PSCredential:{0}; PasswordAuthenticationMechanism:{1}; operationName:{2}; parameterName:{3}.", 0, psCredentials, passwordAuthentication, operationName, parameterName); CimCredential credentials = null; if (psCredentials != null) { NetworkCredential networkCredential = psCredentials.GetNetworkCredential(); DebugHelper.WriteLog("Domain:{0}; UserName:{1}; Password:{2}.", 1, networkCredential.Domain, networkCredential.UserName, psCredentials.Password); credentials = new CimCredential(passwordAuthentication, networkCredential.Domain, networkCredential.UserName, psCredentials.Password); } else { ImpersonatedAuthenticationMechanism impersonatedAuthentication; switch (passwordAuthentication) { case PasswordAuthenticationMechanism.Default: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.None; break; case PasswordAuthenticationMechanism.Negotiate: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.Negotiate; break; case PasswordAuthenticationMechanism.Kerberos: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.Kerberos; break; case PasswordAuthenticationMechanism.NtlmDomain: impersonatedAuthentication = ImpersonatedAuthenticationMechanism.NtlmDomain; break; default: ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return null; } credentials = new CimCredential(impersonatedAuthentication); } DebugHelper.WriteLogEx("return credential {0}", 1, credentials); return credentials; }
/// <summary> /// <para> /// Throw invalid AuthenticationType /// </para> /// </summary> /// <param name="operationName"></param> /// <param name="parameterName"></param> /// <param name="authentication"></param> internal void ThrowInvalidAuthenticationTypeError( string operationName, string parameterName, PasswordAuthenticationMechanism authentication) { string message = String.Format(CultureInfo.CurrentUICulture, Strings.InvalidAuthenticationTypeWithNullCredential, authentication, ImpersonatedAuthenticationMechanism.None, ImpersonatedAuthenticationMechanism.Negotiate, ImpersonatedAuthenticationMechanism.Kerberos, ImpersonatedAuthenticationMechanism.NtlmDomain); PSArgumentOutOfRangeException exception = new PSArgumentOutOfRangeException( parameterName, authentication, message); ThrowTerminatingError(exception, operationName); }
protected override List <ActivityImplementationContext> GetImplementation(NativeActivityContext context) { string empty; string str; string empty1; string str1; typeof(GenericCimCmdletActivity).IsAssignableFrom(base.GetType()); string[] strArrays = this.PSComputerName.Get(context); CimSession[] cimSessionArray = this.CimSession.Get(context); Uri uri = null; if (this.ResourceUri != null) { uri = this.ResourceUri.Get(context); } List <ActivityImplementationContext> activityImplementationContexts = new List <ActivityImplementationContext>(); if (strArrays == null || (int)strArrays.Length <= 0) { ActivityImplementationContext powerShell = this.GetPowerShell(context); CimActivityImplementationContext cimActivityImplementationContext = new CimActivityImplementationContext(powerShell, null, null, null, new AuthenticationMechanism?(AuthenticationMechanism.Default), false, 0, null, null, null, this.ModuleDefinition, uri); activityImplementationContexts.Add(cimActivityImplementationContext); } else { WSManSessionOptions wSManSessionOption = new WSManSessionOptions(); uint?nullable = base.PSActionRunningTimeoutSec.Get(context); if (nullable.HasValue) { wSManSessionOption.Timeout = TimeSpan.FromSeconds((double)((float)nullable.Value)); } bool?nullable1 = this.PSUseSsl.Get(context); bool value = false; if (nullable1.HasValue) { wSManSessionOption.UseSsl = nullable1.Value; value = nullable1.Value; } uint?nullable2 = this.PSPort.Get(context); uint num = 0; if (nullable2.HasValue) { wSManSessionOption.DestinationPort = nullable2.Value; num = nullable2.Value; } PSSessionOption pSSessionOption = this.PSSessionOption.Get(context); if (pSSessionOption != null) { wSManSessionOption.NoEncryption = pSSessionOption.NoEncryption; wSManSessionOption.CertCACheck = pSSessionOption.SkipCACheck; wSManSessionOption.CertCNCheck = pSSessionOption.SkipCNCheck; wSManSessionOption.CertRevocationCheck = pSSessionOption.SkipRevocationCheck; if (pSSessionOption.UseUTF16) { wSManSessionOption.PacketEncoding = PacketEncoding.Utf16; } if (pSSessionOption.Culture != null) { wSManSessionOption.Culture = pSSessionOption.Culture; } if (pSSessionOption.UICulture != null) { wSManSessionOption.UICulture = pSSessionOption.UICulture; } if (pSSessionOption.ProxyCredential != null) { char[] chrArray = new char[1]; chrArray[0] = '\\'; string[] strArrays1 = pSSessionOption.ProxyCredential.UserName.Split(chrArray); if ((int)strArrays1.Length >= 2) { empty = strArrays1[0]; str = strArrays1[1]; } else { empty = string.Empty; str = strArrays1[0]; } wSManSessionOption.AddProxyCredentials(new CimCredential(PSGeneratedCIMActivity.ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(pSSessionOption.ProxyAuthentication), empty, str, pSSessionOption.ProxyCredential.Password)); } ProxyAccessType proxyAccessType = pSSessionOption.ProxyAccessType; if (proxyAccessType == ProxyAccessType.IEConfig) { wSManSessionOption.ProxyType = ProxyType.InternetExplorer; goto Label0; } else if (proxyAccessType == ProxyAccessType.WinHttpConfig) { wSManSessionOption.ProxyType = ProxyType.WinHttp; goto Label0; } else if (proxyAccessType == (ProxyAccessType.IEConfig | ProxyAccessType.WinHttpConfig)) { goto Label0; } else if (proxyAccessType == ProxyAccessType.AutoDetect) { wSManSessionOption.ProxyType = ProxyType.Auto; goto Label0; } } Label0: PSCredential pSCredential = this.PSCredential.Get(context); string str2 = this.PSCertificateThumbprint.Get(context); if (pSCredential == null || str2 == null) { PasswordAuthenticationMechanism cimPasswordAuthenticationMechanism = PasswordAuthenticationMechanism.Default; AuthenticationMechanism? nullable3 = this.PSAuthentication.Get(context); if (nullable3.HasValue) { cimPasswordAuthenticationMechanism = PSGeneratedCIMActivity.ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(nullable3.Value); } if (str2 != null) { wSManSessionOption.AddDestinationCredentials(new CimCredential(CertificateAuthenticationMechanism.Default, str2)); } if (pSCredential != null) { char[] chrArray1 = new char[1]; chrArray1[0] = '\\'; string[] strArrays2 = pSCredential.UserName.Split(chrArray1); if ((int)strArrays2.Length >= 2) { empty1 = strArrays2[0]; str1 = strArrays2[1]; } else { empty1 = string.Empty; str1 = strArrays2[0]; } wSManSessionOption.AddDestinationCredentials(new CimCredential(cimPasswordAuthenticationMechanism, empty1, str1, pSCredential.Password)); } if (cimSessionArray == null || (int)cimSessionArray.Length <= 0) { string[] strArrays3 = strArrays; for (int i = 0; i < (int)strArrays3.Length; i++) { string str3 = strArrays3[i]; ActivityImplementationContext activityImplementationContext = this.GetPowerShell(context); CimActivityImplementationContext cimActivityImplementationContext1 = new CimActivityImplementationContext(activityImplementationContext, str3, pSCredential, str2, nullable3, value, num, pSSessionOption, null, wSManSessionOption, this.ModuleDefinition, uri); activityImplementationContexts.Add(cimActivityImplementationContext1); } } else { CimSession[] cimSessionArray1 = cimSessionArray; for (int j = 0; j < (int)cimSessionArray1.Length; j++) { CimSession cimSession = cimSessionArray1[j]; ActivityImplementationContext powerShell1 = this.GetPowerShell(context); CimActivityImplementationContext cimActivityImplementationContext2 = new CimActivityImplementationContext(powerShell1, cimSession.ComputerName, pSCredential, str2, nullable3, value, num, pSSessionOption, cimSession, wSManSessionOption, this.ModuleDefinition, uri); activityImplementationContexts.Add(cimActivityImplementationContext2); } } } else { throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithPSCertificateThumbPrint); } } return(activityImplementationContexts); }
internal void BuildSessionOptions(out CimSessionOptions outputOptions, out CimCredential outputCredential) { WSManSessionOptions wSManSessionOption; PasswordAuthenticationMechanism authentication; DebugHelper.WriteLogEx(); CimSessionOptions dComSessionOption = null; /* Requires Authentication for Remote Host */ if (this.credential == null && ComputerName != null) { bool requiredAuth = false; foreach (var c in ComputerName) { if (c != null && !c.Equals("localhost", StringComparison.OrdinalIgnoreCase)) { requiredAuth = true; break; } } if (requiredAuth) { TrySetCredentials(); } } if (this.SessionOption != null) { if (this.SessionOption as WSManSessionOptions == null) { dComSessionOption = new DComSessionOptions(this.sessionOption as DComSessionOptions); } else { dComSessionOption = new WSManSessionOptions(this.sessionOption as WSManSessionOptions); } } outputOptions = null; outputCredential = null; if (dComSessionOption != null) { DComSessionOptions dComSessionOption1 = dComSessionOption as DComSessionOptions; if (dComSessionOption1 != null) { bool flag = false; string empty = string.Empty; if (this.CertificateThumbprint != null) { flag = true; empty = "CertificateThumbprint"; } if (this.portSet) { flag = true; empty = "Port"; } if (flag) { base.ThrowConflictParameterWasSet("New-CimSession", empty, "DComSessionOptions"); return; } } } if (this.portSet || this.CertificateThumbprint != null) { if (dComSessionOption == null) { wSManSessionOption = new WSManSessionOptions(); } else { wSManSessionOption = dComSessionOption as WSManSessionOptions; } WSManSessionOptions port = wSManSessionOption; if (this.portSet) { port.DestinationPort = this.Port; this.portSet = false; } if (this.CertificateThumbprint != null) { CimCredential cimCredential = new CimCredential(CertificateAuthenticationMechanism.Default, this.CertificateThumbprint); port.AddDestinationCredentials(cimCredential); } dComSessionOption = port; } if (this.operationTimeoutSet && dComSessionOption != null) { dComSessionOption.Timeout = TimeSpan.FromSeconds((double)((float)this.OperationTimeoutSec)); } if (this.authenticationSet || this.credential != null) { if (this.authenticationSet) { authentication = this.Authentication; } else { authentication = PasswordAuthenticationMechanism.Default; } PasswordAuthenticationMechanism passwordAuthenticationMechanism = authentication; if (this.authenticationSet) { this.authenticationSet = false; } CimCredential cimCredential1 = base.CreateCimCredentials(this.Credential, passwordAuthenticationMechanism, "New-CimSession", "Authentication"); if (cimCredential1 != null) { object[] objArray = new object[1]; objArray [0] = cimCredential1; DebugHelper.WriteLog("Credentials: {0}", 1, objArray); outputCredential = cimCredential1; if (dComSessionOption != null) { object[] objArray1 = new object[1]; objArray1 [0] = dComSessionOption; DebugHelper.WriteLog("Add credentials to option: {0}", 1, objArray1); dComSessionOption.AddDestinationCredentials(cimCredential1); } } else { return; } } object[] objArray2 = new object[1]; objArray2[0] = outputOptions; DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, objArray2); outputOptions = dComSessionOption; }
protected override List <ActivityImplementationContext> GetImplementation(NativeActivityContext context) { bool needRunspace = !typeof(GenericCimCmdletActivity).IsAssignableFrom(this.GetType()); string[] computernames = PSComputerName.Get(context); CimSession[] sessions = this.CimSession.Get(context); Uri resourceUri = null; if (ResourceUri != null) { resourceUri = ResourceUri.Get(context); } List <ActivityImplementationContext> commands = new List <ActivityImplementationContext>(); // Configure the remote connectivity options... if (computernames != null && computernames.Length > 0) { WSManSessionOptions sessionOptions = new WSManSessionOptions(); // Set a timeout on the connection... uint?timeout = PSActionRunningTimeoutSec.Get(context); if (timeout.HasValue) { sessionOptions.Timeout = TimeSpan.FromSeconds((double)(timeout.Value)); } // See if we should use SSL or not... bool?useSsl = PSUseSsl.Get(context); bool sessionOptionUseSsl = false; if (useSsl.HasValue) { sessionOptions.UseSsl = useSsl.Value; sessionOptionUseSsl = useSsl.Value; } // Set the port to use uint?port = PSPort.Get(context); uint sessionOptionPort = 0; if (port.HasValue) { sessionOptions.DestinationPort = port.Value; sessionOptionPort = port.Value; } // Map over options from PSSessionConfig to WSManSessionOptions PSSessionOption pso = PSSessionOption.Get(context); if (pso != null) { sessionOptions.NoEncryption = pso.NoEncryption; sessionOptions.CertCACheck = pso.SkipCACheck; sessionOptions.CertCNCheck = pso.SkipCNCheck; sessionOptions.CertRevocationCheck = pso.SkipRevocationCheck; if (pso.UseUTF16) { sessionOptions.PacketEncoding = PacketEncoding.Utf16; } if (pso.Culture != null) { sessionOptions.Culture = pso.Culture; } if (pso.UICulture != null) { sessionOptions.UICulture = pso.UICulture; } if (pso.ProxyCredential != null) { string[] parts = pso.ProxyCredential.UserName.Split('\\'); string domain, userid; if (parts.Length < 2) { domain = string.Empty; userid = parts[0]; } else { domain = parts[0]; userid = parts[1]; } sessionOptions.AddProxyCredentials( new CimCredential(ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(pso.ProxyAuthentication), domain, userid, pso.ProxyCredential.Password)); } switch (pso.ProxyAccessType) { case ProxyAccessType.WinHttpConfig: sessionOptions.ProxyType = ProxyType.WinHttp; break; case ProxyAccessType.AutoDetect: sessionOptions.ProxyType = ProxyType.Auto; break; case ProxyAccessType.IEConfig: sessionOptions.ProxyType = ProxyType.InternetExplorer; break; } } PSCredential pscreds = PSCredential.Get(context); string certificateThumbprint = PSCertificateThumbprint.Get(context); if (pscreds != null && certificateThumbprint != null) { throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithPSCertificateThumbPrint); } PasswordAuthenticationMechanism passwordAuthenticationMechanism = PasswordAuthenticationMechanism.Default; AuthenticationMechanism? authenticationMechanism = PSAuthentication.Get(context); if (authenticationMechanism.HasValue) { passwordAuthenticationMechanism = ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(authenticationMechanism.Value); } if (certificateThumbprint != null) { sessionOptions.AddDestinationCredentials(new CimCredential(CertificateAuthenticationMechanism.Default, certificateThumbprint)); } if (pscreds != null) { string[] parts = pscreds.UserName.Split('\\'); string domain, userid; if (parts.Length < 2) { domain = string.Empty; userid = parts[0]; } else { domain = parts[0]; userid = parts[1]; } sessionOptions.AddDestinationCredentials(new CimCredential(passwordAuthenticationMechanism, domain, userid, pscreds.Password)); } // Create the PowerShell instance, and add the script to it. if (sessions != null && sessions.Length > 0) { foreach (CimSession session in sessions) { ActivityImplementationContext configuredCommand = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext( configuredCommand, session.ComputerName, pscreds, certificateThumbprint, authenticationMechanism, sessionOptionUseSsl, sessionOptionPort, pso, session, sessionOptions, ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); //if (needRunspace) // GetRunspaceForCimCmdlet(context, activityImplementationContext); } } else if (this.PSCommandName.Equals("CimCmdlets\\New-CimSession", StringComparison.OrdinalIgnoreCase)) { // NewCimSession activity is a special one as it creates the required sessions based on number of computers specified in one go. ActivityImplementationContext baseContext = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext(baseContext, null, // ComputerName pscreds, certificateThumbprint, authenticationMechanism, sessionOptionUseSsl, sessionOptionPort, pso, null, // session sessionOptions, ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); } else { foreach (string computer in computernames) { ActivityImplementationContext baseContext = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext(baseContext, computer, pscreds, certificateThumbprint, authenticationMechanism, sessionOptionUseSsl, sessionOptionPort, pso, null, // session sessionOptions, ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); } } } // Configure the local invocation options else { // Create the PowerShell instance, and add the script to it. ActivityImplementationContext baseContext = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext(baseContext, null, // ComputerName null, // Credential null, // CertificateThumbprint AuthenticationMechanism.Default, false, // UseSsl 0, // Port null, // PSSessionOption null, // Session null, // CimSessionOptions ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); } return(commands); }
internal void BuildSessionOptions (out CimSessionOptions outputOptions, out CimCredential outputCredential) { WSManSessionOptions wSManSessionOption; PasswordAuthenticationMechanism authentication; DebugHelper.WriteLogEx (); CimSessionOptions dComSessionOption = null; /* Requires Authentication for Remote Host */ if (this.credential == null && ComputerName != null) { bool requiredAuth = false; foreach(var c in ComputerName) { if (c != null && !c.Equals("localhost", StringComparison.OrdinalIgnoreCase)) { requiredAuth = true; break; } } if (requiredAuth) { TrySetCredentials(); } } if (this.SessionOption != null) { if (this.SessionOption as WSManSessionOptions == null) { dComSessionOption = new DComSessionOptions (this.sessionOption as DComSessionOptions); } else { dComSessionOption = new WSManSessionOptions (this.sessionOption as WSManSessionOptions); } } outputOptions = null; outputCredential = null; if (dComSessionOption != null) { DComSessionOptions dComSessionOption1 = dComSessionOption as DComSessionOptions; if (dComSessionOption1 != null) { bool flag = false; string empty = string.Empty; if (this.CertificateThumbprint != null) { flag = true; empty = "CertificateThumbprint"; } if (this.portSet) { flag = true; empty = "Port"; } if (flag) { base.ThrowConflictParameterWasSet ("New-CimSession", empty, "DComSessionOptions"); return; } } } if (this.portSet || this.CertificateThumbprint != null) { if (dComSessionOption == null) { wSManSessionOption = new WSManSessionOptions (); } else { wSManSessionOption = dComSessionOption as WSManSessionOptions; } WSManSessionOptions port = wSManSessionOption; if (this.portSet) { port.DestinationPort = this.Port; this.portSet = false; } if (this.CertificateThumbprint != null) { CimCredential cimCredential = new CimCredential (CertificateAuthenticationMechanism.Default, this.CertificateThumbprint); port.AddDestinationCredentials (cimCredential); } dComSessionOption = port; } if (this.operationTimeoutSet && dComSessionOption != null) { dComSessionOption.Timeout = TimeSpan.FromSeconds ((double)((float)this.OperationTimeoutSec)); } if (this.authenticationSet || this.credential != null) { if (this.authenticationSet) { authentication = this.Authentication; } else { authentication = PasswordAuthenticationMechanism.Default; } PasswordAuthenticationMechanism passwordAuthenticationMechanism = authentication; if (this.authenticationSet) { this.authenticationSet = false; } CimCredential cimCredential1 = base.CreateCimCredentials (this.Credential, passwordAuthenticationMechanism, "New-CimSession", "Authentication"); if (cimCredential1 != null) { object[] objArray = new object[1]; objArray [0] = cimCredential1; DebugHelper.WriteLog ("Credentials: {0}", 1, objArray); outputCredential = cimCredential1; if (dComSessionOption != null) { object[] objArray1 = new object[1]; objArray1 [0] = dComSessionOption; DebugHelper.WriteLog ("Add credentials to option: {0}", 1, objArray1); dComSessionOption.AddDestinationCredentials (cimCredential1); } } else { return; } } object[] objArray2 = new object[1]; objArray2[0] = outputOptions; DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, objArray2); outputOptions = dComSessionOption; }
internal CimCredential CreateCimCredentials(PSCredential psCredentials, PasswordAuthenticationMechanism passwordAuthentication, string operationName, string parameterName) { CimCredential cimCredential; ImpersonatedAuthenticationMechanism impersonatedAuthenticationMechanism; object[] objArray = new object[4]; objArray[0] = psCredentials; objArray[1] = passwordAuthentication; objArray[2] = operationName; objArray[3] = parameterName; DebugHelper.WriteLogEx("PSCredential:{0}; PasswordAuthenticationMechanism:{1}; operationName:{2}; parameterName:{3}.", 0, objArray); if (psCredentials == null) { PasswordAuthenticationMechanism passwordAuthenticationMechanism = passwordAuthentication; if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Default) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.None; } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Digest || passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Basic) { this.ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return null; } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Negotiate) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.Negotiate; } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Kerberos) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.Kerberos; } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.NtlmDomain) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.NtlmDomain; } else { this.ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return null; } cimCredential = new CimCredential(impersonatedAuthenticationMechanism); } else { NetworkCredential networkCredential = psCredentials.GetNetworkCredential(); /* Send directly PSCredentials SecurePassword if NetworkCredentials password is only encrypted */ object[] domain = new object[3]; domain[0] = networkCredential.Domain; domain[1] = networkCredential.UserName; domain[2] = networkCredential.SecurePassword ?? psCredentials.Password; DebugHelper.WriteLog("Domain:{0}; UserName:{1}; Password:{2}.", 1, domain); cimCredential = new CimCredential(passwordAuthentication, networkCredential.Domain, networkCredential.UserName, networkCredential.SecurePassword ?? psCredentials.Password); } object[] objArray1 = new object[1]; objArray1[0] = cimCredential; DebugHelper.WriteLogEx("return credential {0}", 1, objArray1); return cimCredential; this.ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return null; }
internal static void CreateCimCredential(PasswordAuthenticationMechanism authenticationMechanism, string domain, string userName, SecureString password, out NativeCimCredential credential) { // TODO: Implement credential = new NativeCimCredential(true, new SecureString()); }
public CimCredential(PasswordAuthenticationMechanism authenticationMechanism, string domain, string userName, SecureString password) { if (userName != null) { string authTypeCredSSP = null; if (authenticationMechanism != PasswordAuthenticationMechanism.Default) { if (authenticationMechanism != PasswordAuthenticationMechanism.Basic) { if (authenticationMechanism != PasswordAuthenticationMechanism.Digest) { if (authenticationMechanism != PasswordAuthenticationMechanism.Negotiate) { if (authenticationMechanism != PasswordAuthenticationMechanism.Kerberos) { if (authenticationMechanism != PasswordAuthenticationMechanism.NtlmDomain) { if (authenticationMechanism != PasswordAuthenticationMechanism.CredSsp) { throw new ArgumentOutOfRangeException("authenticationMechanism"); } else { authTypeCredSSP = AuthType.AuthTypeCredSSP; } } else { authTypeCredSSP = AuthType.AuthTypeNTLM; } } else { authTypeCredSSP = AuthType.AuthTypeKerberos; } } else { authTypeCredSSP = AuthType.AuthTypeNegoWithCredentials; } } else { authTypeCredSSP = AuthType.AuthTypeDigest; } } else { authTypeCredSSP = AuthType.AuthTypeBasic; } } else { authTypeCredSSP = AuthType.AuthTypeDefault; } NativeCimCredential.CreateCimCredential(authTypeCredSSP, domain, userName, password, out this.credential); return; } else { throw new ArgumentNullException("userName"); } }
public RDPCredential(PasswordAuthenticationMechanism passAuth, PSCredential psCreds) : base(passAuth, ParseDomain(psCreds), ParseUser(psCreds), psCreds.Password) { }
public RDPCredential(PasswordAuthenticationMechanism passAuth, string domain, string userName, SecureString password) : base(passAuth, domain, userName, password) { }
internal void ThrowInvalidAuthenticationTypeError(string operationName, string parameterName, PasswordAuthenticationMechanism authentication) { object[] objArray = new object[5]; objArray[0] = authentication; objArray[1] = ImpersonatedAuthenticationMechanism.None; objArray[2] = ImpersonatedAuthenticationMechanism.Negotiate; objArray[3] = ImpersonatedAuthenticationMechanism.Kerberos; objArray[4] = ImpersonatedAuthenticationMechanism.NtlmDomain; string str = string.Format(CultureInfo.CurrentUICulture, Strings.InvalidAuthenticationTypeWithNullCredential, objArray); PSArgumentOutOfRangeException pSArgumentOutOfRangeException = new PSArgumentOutOfRangeException(parameterName, (object)authentication, str); this.ThrowTerminatingError(pSArgumentOutOfRangeException, operationName); }
static DnsProviderMSDNSProvider() { _sentinel = PasswordAuthenticationMechanism.Kerberos; }
public WSManHelper(string ComputerName, PasswordAuthenticationMechanism AuthType, string Domain, string UserName, SecureString Password, uint Port = 5985) { ClassSetup(ComputerName, AuthType, Domain, UserName, Password, Port); }
private static CimCredential GetCimCredentials(PasswordAuthenticationMechanism authenticationMechanism, PSCredential credential) { NetworkCredential networkCredential = credential.GetNetworkCredential(); return(new CimCredential(authenticationMechanism, networkCredential.Domain, networkCredential.UserName, credential.Password)); }
internal CimCredential CreateCimCredentials(PSCredential psCredentials, PasswordAuthenticationMechanism passwordAuthentication, string operationName, string parameterName) { CimCredential cimCredential; ImpersonatedAuthenticationMechanism impersonatedAuthenticationMechanism; object[] objArray = new object[4]; objArray[0] = psCredentials; objArray[1] = passwordAuthentication; objArray[2] = operationName; objArray[3] = parameterName; DebugHelper.WriteLogEx("PSCredential:{0}; PasswordAuthenticationMechanism:{1}; operationName:{2}; parameterName:{3}.", 0, objArray); if (psCredentials == null) { PasswordAuthenticationMechanism passwordAuthenticationMechanism = passwordAuthentication; if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Default) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.None; } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Digest || passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Basic) { this.ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return(null); } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Negotiate) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.Negotiate; } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.Kerberos) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.Kerberos; } else if (passwordAuthenticationMechanism == PasswordAuthenticationMechanism.NtlmDomain) { impersonatedAuthenticationMechanism = ImpersonatedAuthenticationMechanism.NtlmDomain; } else { this.ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return(null); } cimCredential = new CimCredential(impersonatedAuthenticationMechanism); } else { NetworkCredential networkCredential = psCredentials.GetNetworkCredential(); /* Send directly PSCredentials SecurePassword if NetworkCredentials password is only encrypted */ object[] domain = new object[3]; domain[0] = networkCredential.Domain; domain[1] = networkCredential.UserName; domain[2] = networkCredential.SecurePassword ?? psCredentials.Password; DebugHelper.WriteLog("Domain:{0}; UserName:{1}; Password:{2}.", 1, domain); cimCredential = new CimCredential(passwordAuthentication, networkCredential.Domain, networkCredential.UserName, networkCredential.SecurePassword ?? psCredentials.Password); } object[] objArray1 = new object[1]; objArray1[0] = cimCredential; DebugHelper.WriteLogEx("return credential {0}", 1, objArray1); return(cimCredential); this.ThrowInvalidAuthenticationTypeError(operationName, parameterName, passwordAuthentication); return(null); }
/// <summary> /// Build a CimSessionOptions, used to create CimSession. /// </summary> /// <returns>Null means no prefer CimSessionOptions.</returns> internal void BuildSessionOptions(out CimSessionOptions outputOptions, out CimCredential outputCredential) { DebugHelper.WriteLogEx(); CimSessionOptions options = null; if (this.SessionOption != null) { // clone the sessionOption object if (this.SessionOption is WSManSessionOptions) { options = new WSManSessionOptions(this.SessionOption as WSManSessionOptions); } else { options = new DComSessionOptions(this.SessionOption as DComSessionOptions); } } outputOptions = null; outputCredential = null; if (options != null) { DComSessionOptions dcomOptions = (options as DComSessionOptions); if (dcomOptions != null) { bool conflict = false; string parameterName = string.Empty; if (this.CertificateThumbprint != null) { conflict = true; parameterName = @"CertificateThumbprint"; } if (portSet) { conflict = true; parameterName = @"Port"; } if (conflict) { ThrowConflictParameterWasSet(@"New-CimSession", parameterName, @"DComSessionOptions"); return; } } } if (portSet || (this.CertificateThumbprint != null)) { WSManSessionOptions wsmanOptions = (options == null) ? new WSManSessionOptions() : options as WSManSessionOptions; if (portSet) { wsmanOptions.DestinationPort = this.Port; portSet = false; } if (this.CertificateThumbprint != null) { CimCredential credentials = new(CertificateAuthenticationMechanism.Default, this.CertificateThumbprint); wsmanOptions.AddDestinationCredentials(credentials); } options = wsmanOptions; } if (this.operationTimeoutSet) { if (options != null) { options.Timeout = TimeSpan.FromSeconds((double)this.OperationTimeoutSec); } } if (this.authenticationSet || (this.Credential != null)) { PasswordAuthenticationMechanism authentication = this.authenticationSet ? this.Authentication : PasswordAuthenticationMechanism.Default; if (this.authenticationSet) { this.authenticationSet = false; } CimCredential credentials = CreateCimCredentials(this.Credential, authentication, @"New-CimSession", @"Authentication"); if (credentials == null) { return; } DebugHelper.WriteLog("Credentials: {0}", 1, credentials); outputCredential = credentials; if (options != null) { DebugHelper.WriteLog("Add credentials to option: {0}", 1, options); options.AddDestinationCredentials(credentials); } } DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, outputOptions); outputOptions = options; }
public CimCredential(PasswordAuthenticationMechanism authenticationMechanism, string domain, string userName, SecureString password) { if (userName != null) { string authTypeCredSSP = null; if (authenticationMechanism != PasswordAuthenticationMechanism.Default) { if (authenticationMechanism != PasswordAuthenticationMechanism.Basic) { if (authenticationMechanism != PasswordAuthenticationMechanism.Digest) { if (authenticationMechanism != PasswordAuthenticationMechanism.Negotiate) { if (authenticationMechanism != PasswordAuthenticationMechanism.Kerberos) { if (authenticationMechanism != PasswordAuthenticationMechanism.NtlmDomain) { if (authenticationMechanism != PasswordAuthenticationMechanism.CredSsp) { throw new ArgumentOutOfRangeException("authenticationMechanism"); } else { authTypeCredSSP = AuthType.AuthTypeCredSSP; } } else { authTypeCredSSP = AuthType.AuthTypeNTLM; } } else { authTypeCredSSP = AuthType.AuthTypeKerberos; } } else { authTypeCredSSP = AuthType.AuthTypeNegoWithCredentials; } } else { authTypeCredSSP = AuthType.AuthTypeDigest; } } else { authTypeCredSSP = AuthType.AuthTypeBasic; } } else { authTypeCredSSP = AuthType.AuthTypeDefault; } NativeCimCredential.CreateCimCredential(authTypeCredSSP, domain, userName, password, out this.credential); return; } else { throw new ArgumentNullException("userName"); } }