internal IWSManSession CreateSessionObject(IWSManEx wsmanObject, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string connectionString, string certificateThumbprint, bool usessl) { WSManHelper.ValidateSpecifiedAuthentication(authentication, credential, certificateThumbprint); int num = 0; if (authentication.ToString() != null) { if (authentication.Equals(AuthenticationMechanism.None)) { num = num | 0x8000; } if (authentication.Equals(AuthenticationMechanism.Basic)) { num = num | 0x40000 | 0x1000; } if (authentication.Equals(AuthenticationMechanism.Negotiate)) { num = num | 0x20000; } if (authentication.Equals(AuthenticationMechanism.Kerberos)) { num = num | 0x80000; } if (authentication.Equals(AuthenticationMechanism.Digest)) { num = num | 0x10000 | 0x1000; } if (authentication.Equals(AuthenticationMechanism.Credssp)) { num = num | 0x1000000 | 0x1000; } if (authentication.Equals(AuthenticationMechanism.ClientCertificate)) { num = num | 0x200000; } } IWSManConnectionOptionsEx2 userName = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions(); if (credential != null) { if (credential.UserName != null) { NetworkCredential networkCredential = credential.GetNetworkCredential(); if (!string.IsNullOrEmpty(networkCredential.Domain)) { userName.UserName = string.Concat(networkCredential.Domain, "\\", networkCredential.UserName); } else { if (authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic)) { userName.UserName = networkCredential.UserName; } else { userName.UserName = string.Concat("\\", networkCredential.UserName); } } userName.Password = networkCredential.Password; if (!authentication.Equals(AuthenticationMechanism.Credssp) || !authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic)) { num = num | 0x1000; } } } if (certificateThumbprint != null) { userName.CertificateThumbprint = certificateThumbprint; num = num | 0x200000; } if (sessionoption == null) { num = num | 1; } else { if (sessionoption.ProxyAuthentication != 0) { int num1 = 0; int num2 = 0; if (!sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyIEConfig)) { if (!sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyAutoDetect)) { if (!sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyNoProxyServer)) { if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyWinHttpConfig)) { num1 = userName.ProxyWinHttpConfig(); } } else { num1 = userName.ProxyNoProxyServer(); } } else { num1 = userName.ProxyAutoDetect(); } } else { num1 = userName.ProxyIEConfig(); } if (!sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Basic)) { if (!sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Negotiate)) { if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Digest)) { num2 = userName.ProxyAuthenticationUseDigest(); } } else { num2 = userName.ProxyAuthenticationUseNegotiate(); } } else { num2 = userName.ProxyAuthenticationUseBasic(); } if (sessionoption.ProxyCredential == null) { userName.SetProxy((int)sessionoption.ProxyAccessType, (int)sessionoption.ProxyAuthentication, null, null); } else { try { userName.SetProxy(num1, num2, sessionoption.ProxyCredential.UserName, sessionoption.ProxyCredential.Password); } catch (Exception exception1) { Exception exception = exception1; this.AssertError(exception.Message, false, null); } } } if (sessionoption.SkipCACheck) { num = num | 0x2000; } if (sessionoption.SkipCNCheck) { num = num | 0x4000; } if (sessionoption.SPNPort > 0) { num = num | 0x400000; } if (!sessionoption.UseUtf16) { num = num | 1; } else { num = num | 0x800000; } if (!sessionoption.UseEncryption) { num = num | 0x100000; } if (sessionoption.SkipRevocationCheck) { num = num | 0x2000000; } } if (usessl) { num = num | 0x8000000; } IWSManSession operationTimeout = null; try { operationTimeout = (IWSManSession)wsmanObject.CreateSession(connectionString, num, userName); if (sessionoption != null && sessionoption.OperationTimeout > 0) { operationTimeout.Timeout = sessionoption.OperationTimeout; } } catch (COMException cOMException1) { COMException cOMException = cOMException1; this.AssertError(cOMException.Message, false, null); } return operationTimeout; }
internal IWSManSession CreateSessionObject(IWSManEx wsmanObject, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string connectionString, string certificateThumbprint, bool usessl) { ValidateSpecifiedAuthentication(authentication, credential, certificateThumbprint); ////if authentication is given int sessionFlags = 0; if (authentication.ToString() != null) { if (authentication.Equals(AuthenticationMechanism.None)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNoAuthentication; } if (authentication.Equals(AuthenticationMechanism.Basic)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseBasic | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } if (authentication.Equals(AuthenticationMechanism.Negotiate)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNegotiate; } if (authentication.Equals(AuthenticationMechanism.Kerberos)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseKerberos; } if (authentication.Equals(AuthenticationMechanism.Digest)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseDigest | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } if (authentication.Equals(AuthenticationMechanism.Credssp)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseCredSsp | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } if (authentication.Equals(AuthenticationMechanism.ClientCertificate)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate; } } IWSManConnectionOptionsEx2 connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions(); if (credential != null) { //connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions(); System.Net.NetworkCredential nwCredential = new System.Net.NetworkCredential(); if (credential.UserName != null) { nwCredential = credential.GetNetworkCredential(); if (String.IsNullOrEmpty(nwCredential.Domain)) { if (authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic)) { connObject.UserName = nwCredential.UserName; } else { // just wanted to not use null domain, empty is actually fine connObject.UserName = "******" + nwCredential.UserName; } } else { connObject.UserName = nwCredential.Domain + "\\" + nwCredential.UserName; } connObject.Password = nwCredential.Password; if (!authentication.Equals(AuthenticationMechanism.Credssp) || !authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } } } if (certificateThumbprint != null) { connObject.CertificateThumbprint = certificateThumbprint; sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate; } if (sessionoption != null) { if (sessionoption.ProxyAuthentication != 0) { int ProxyAccessflags = 0; int ProxyAuthenticationFlags = 0; if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyIEConfig)) { ProxyAccessflags = connObject.ProxyIEConfig(); } else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyAutoDetect)) { ProxyAccessflags = connObject.ProxyAutoDetect(); } else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyNoProxyServer)) { ProxyAccessflags = connObject.ProxyNoProxyServer(); } else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyWinHttpConfig)) { ProxyAccessflags = connObject.ProxyWinHttpConfig(); } if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Basic)) { ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseBasic(); } else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Negotiate)) { ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseNegotiate(); } else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Digest)) { ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseDigest(); } if (sessionoption.ProxyCredential != null) { try { connObject.SetProxy(ProxyAccessflags, ProxyAuthenticationFlags, sessionoption.ProxyCredential.UserName, sessionoption.ProxyCredential.Password); } catch (Exception ex) { AssertError(ex.Message, false, null); } } else { connObject.SetProxy((int)sessionoption.ProxyAccessType, (int)sessionoption.ProxyAuthentication, null, null); } } if (sessionoption.SkipCACheck) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCACheck; } if (sessionoption.SkipCNCheck) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCNCheck; } if (sessionoption.SPNPort > 0) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagEnableSpnServerPort; } if (sessionoption.UseUtf16) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf16; } else { //If UseUtf16 is false, then default Encoding is Utf8 sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8; } if (!sessionoption.UseEncryption) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagNoEncryption; } if (sessionoption.SkipRevocationCheck) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipRevocationCheck; } } else { //If SessionOption is null then, default Encoding is Utf8 sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8; } if (usessl) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseSsl; } IWSManSession m_SessionObj = null; try { m_SessionObj = (IWSManSession)wsmanObject.CreateSession(connectionString, sessionFlags, connObject); if (sessionoption != null) { if (sessionoption.OperationTimeout > 0) { m_SessionObj.Timeout = sessionoption.OperationTimeout; } } } catch (COMException ex) { AssertError(ex.Message, false, null); } return(m_SessionObj); }
internal IWSManSession CreateSessionObject(IWSManEx wsmanObject, AuthenticationMechanism authentication, SessionOption sessionoption, PSCredential credential, string connectionString, string certificateThumbprint, bool usessl) { ValidateSpecifiedAuthentication(authentication, credential, certificateThumbprint); ////if authentication is given int sessionFlags = 0; if (authentication.ToString() != null) { if (authentication.Equals(AuthenticationMechanism.None)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNoAuthentication; } if (authentication.Equals(AuthenticationMechanism.Basic)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseBasic | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } if (authentication.Equals(AuthenticationMechanism.Negotiate)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseNegotiate; } if (authentication.Equals(AuthenticationMechanism.Kerberos)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseKerberos; } if (authentication.Equals(AuthenticationMechanism.Digest)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseDigest | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } if (authentication.Equals(AuthenticationMechanism.Credssp)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseCredSsp | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } if (authentication.Equals(AuthenticationMechanism.ClientCertificate)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate; } } IWSManConnectionOptionsEx2 connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions(); if (credential != null) { //connObject = (IWSManConnectionOptionsEx2)wsmanObject.CreateConnectionOptions(); System.Net.NetworkCredential nwCredential = new System.Net.NetworkCredential(); if (credential.UserName != null) { nwCredential = credential.GetNetworkCredential(); if (String.IsNullOrEmpty(nwCredential.Domain)) { if ( authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic) ) { connObject.UserName = nwCredential.UserName; } else { // just wanted to not use null domain, empty is actually fine connObject.UserName = "******" + nwCredential.UserName; } } else { connObject.UserName = nwCredential.Domain + "\\" + nwCredential.UserName; } connObject.Password = nwCredential.Password; if (!authentication.Equals(AuthenticationMechanism.Credssp) || !authentication.Equals(AuthenticationMechanism.Digest) || authentication.Equals(AuthenticationMechanism.Basic)) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagCredUserNamePassword; } } } if (certificateThumbprint != null) { connObject.CertificateThumbprint = certificateThumbprint; sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseClientCertificate; } if (sessionoption != null) { if (sessionoption.ProxyAuthentication != 0) { int ProxyAccessflags = 0; int ProxyAuthenticationFlags = 0; if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyIEConfig)) { ProxyAccessflags = connObject.ProxyIEConfig(); } else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyAutoDetect)) { ProxyAccessflags = connObject.ProxyAutoDetect(); } else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyNoProxyServer)) { ProxyAccessflags = connObject.ProxyNoProxyServer(); } else if (sessionoption.ProxyAccessType.Equals(ProxyAccessType.ProxyWinHttpConfig)) { ProxyAccessflags = connObject.ProxyWinHttpConfig(); } if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Basic)) { ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseBasic(); } else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Negotiate)) { ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseNegotiate(); } else if (sessionoption.ProxyAuthentication.Equals(ProxyAuthentication.Digest)) { ProxyAuthenticationFlags = connObject.ProxyAuthenticationUseDigest(); } if (sessionoption.ProxyCredential != null) { try { connObject.SetProxy(ProxyAccessflags, ProxyAuthenticationFlags, sessionoption.ProxyCredential.UserName, sessionoption.ProxyCredential.Password); } catch (Exception ex) { AssertError(ex.Message, false, null); } } else { connObject.SetProxy((int)sessionoption.ProxyAccessType, (int)sessionoption.ProxyAuthentication, null, null); } } if (sessionoption.SkipCACheck) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCACheck; } if (sessionoption.SkipCNCheck) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipCNCheck; } if (sessionoption.SPNPort > 0) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagEnableSpnServerPort; } if (sessionoption.UseUtf16) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf16; } else { //If UseUtf16 is false, then default Encoding is Utf8 sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8; } if (!sessionoption.UseEncryption) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagNoEncryption; } if (sessionoption.SkipRevocationCheck) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagSkipRevocationCheck; } } else { //If SessionOption is null then, default Encoding is Utf8 sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUtf8; } if (usessl) { sessionFlags = sessionFlags | (int)WSManSessionFlags.WSManFlagUseSsl; } IWSManSession m_SessionObj = null; try { m_SessionObj = (IWSManSession)wsmanObject.CreateSession(connectionString, sessionFlags, connObject); if (sessionoption != null) { if (sessionoption.OperationTimeout > 0) { m_SessionObj.Timeout = sessionoption.OperationTimeout; } } } catch (COMException ex) { AssertError(ex.Message, false, null); } return m_SessionObj; }