Пример #1
0
        /// <summary>
        /// Initialize the securityMechContext based on the security package type
        /// </summary>
        /// <param name="mechType">security mechanism type</param>
        /// <param name="inToken">the input security token</param>
        /// <exception cref="InvalidOperationException">Thrown if could not find the configuration.</exception>
        /// <exception cref="InvalidOperationException">Thrown when security configuration is unknown</exception>
        private void InitializeSecurityContext(MechType mechType, byte[] inToken)
        {
            SpngClientContext   clientContext = this.client.Context as SpngClientContext;
            SecurityPackageType authType      = SpngUtility.ConvertMechType(mechType);

            CurrentSecurityConfig = SpngUtility.GetSecurityConfig(this.securityConfigList, authType);

            if (CurrentSecurityConfig == null)
            {
                throw new InvalidOperationException("Missing configuration for " + authType.ToString());
            }

            if (securityMechContext != null)
            {
                // re-enter. Nothing need to do
                return;
            }

            if (CurrentSecurityConfig.GetType() == typeof(KerberosClientSecurityConfig))
            {
                KerberosClientSecurityConfig kileConfig = CurrentSecurityConfig as KerberosClientSecurityConfig;

                securityMechContext = new KerberosClientSecurityContext(
                    kileConfig.ServiceName,
                    kileConfig.ClientCredential,
                    KerberosAccountType.User,
                    kileConfig.KdcIpAddress,
                    kileConfig.KdcPort,
                    kileConfig.TransportType,
                    kileConfig.SecurityAttributes);
            }
            else if (CurrentSecurityConfig.GetType() == typeof(NlmpClientSecurityConfig))
            {
                NlmpClientSecurityConfig nlmpConfig = CurrentSecurityConfig as NlmpClientSecurityConfig;

                NlmpClientCredential cred = new NlmpClientCredential(
                    nlmpConfig.TargetName,
                    nlmpConfig.DomainName,
                    nlmpConfig.AccountName,
                    nlmpConfig.Password);
                securityMechContext = new NlmpClientSecurityContext(cred, nlmpConfig.SecurityAttributes);
            }
            else if (CurrentSecurityConfig.GetType() == typeof(SspiClientSecurityConfig))
            {
                throw new InvalidOperationException("Only support Kerberos security config and NTLM security config");
            }
            else
            {
                throw new InvalidOperationException("unknown security config");
            }
        }
        static void Main(string[] args)
        {
            SwnClient client       = new SwnClient();
            string    serverName   = "GeneralFS";
            string    serverAddr   = "192.168.1.200";
            string    resourceName = "GeneralFS";
            string    clientName   = Guid.NewGuid().ToString();

            int                   retVar            = 0;
            TimeSpan              timeOut           = new TimeSpan(0, 0, 10);
            AccountCredential     accountCredential = new AccountCredential("contoso.com", "Administrator", "Password01!");
            NlmpClientCredential  nlmpCredential    = new NlmpClientCredential(serverName, "contoso.com", "Administrator", "Password01!");
            ClientSecurityContext securityContext   = new NlmpClientSecurityContext(nlmpCredential);

            //Bind to server
            client.SwnBind(serverName, accountCredential, securityContext,
                           RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_PKT_INTEGRITY, timeOut);

            //Get interface list
            WITNESS_INTERFACE_LIST interfaceList;

            try
            {
                retVar = client.WitnessGetInterfaceList(out interfaceList);
                Console.WriteLine("Call WitnessGetInterfaceList: " + retVar);
            }
            catch (TimeoutException)
            {
                client.SwnUnbind(timeOut);
                return;
            }

            string swnServerName = "";

            foreach (var info in interfaceList.InterfaceInfo)
            {
                if ((info.Flags & (uint)SwnNodeFlagsValue.INTERFACE_WITNESS) != 0)
                {
                    if ((info.Flags & (uint)SwnNodeFlagsValue.IPv4) != 0)
                    {
                        swnServerName = (new IPAddress(info.IPV4)).ToString();
                    }
                    else if ((info.Flags & (uint)SwnNodeFlagsValue.IPv6) != 0)
                    {
                        byte[] ipv6 = new byte[16];
                        Buffer.BlockCopy(info.IPV6, 0, ipv6, 0, 16);
                        swnServerName = (new IPAddress(ipv6)).ToString();
                    }
                    else
                    {
                        throw new ArgumentException();
                    }
                    break;
                }
            }

            SwnClient client2 = new SwnClient();

            client2.SwnBind(swnServerName, accountCredential, securityContext,
                            RpceAuthenticationLevel.RPC_C_AUTHN_LEVEL_PKT_INTEGRITY, timeOut);

            //Registration
            IntPtr pContext;

            retVar = client2.WitnessRegister(SwnVersion.SWN_VERSION_1, resourceName, serverAddr, clientName, out pContext);
            Console.WriteLine("Call WitnessRegister: " + retVar);

            uint callId = 0;

            try
            {
                RESP_ASYNC_NOTIFY respNotify;
                callId = client2.WitnessAsyncNotify(pContext);
                Console.WriteLine("Call WitnessAsyncNotify: " + callId);

                retVar = client2.WitnessAsyncNotifyExpect(callId, out respNotify);
                Console.WriteLine("Call WitnessAsyncNotify: " + retVar);
                Console.WriteLine("NumberOfMessages: " + respNotify.NumberOfMessages);
                Console.WriteLine("Length: " + respNotify.Length);
                PrintNotification(respNotify);

                callId = client2.WitnessAsyncNotify(pContext);
                Console.WriteLine("Call WitnessAsyncNotify: " + callId);

                retVar = client2.WitnessAsyncNotifyExpect(callId, out respNotify);
                Console.WriteLine("Call WitnessAsyncNotify: " + retVar);
                Console.WriteLine("NumberOfMessages: " + respNotify.NumberOfMessages);
                Console.WriteLine("Length: " + respNotify.Length);
                PrintNotification(respNotify);
            }
            catch (TimeoutException)
            {
                Console.WriteLine("Throw a TimeoutException.");
            }

            //UnRegistration
            retVar = client2.WitnessUnRegister(pContext);
            Console.WriteLine("Call WitnessUnRegister: " + retVar);

            client2.SwnUnbind(timeOut);
            client.SwnUnbind(timeOut);

            Console.ReadKey();
        }