示例#1
0
        public override void f_INIT()
        {
            Tracer.WriteLine("J{0}_{1} {2} -> INIT", this.f_getId(), this.Type, this.GetType().Name);

            try
            {
                //// Add an endpoint so the client can connect, this is local-host only:
                server.AddProtocol(RpcProtseq.ncalrpc, codeAPI, 100);
                //// If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\" + codeAPI, 25);
                ////// If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true
                server.AddProtocol(RpcProtseq.ncacn_ip_tcp, @"18081", 25);

                // Add the types of authentication we will accept
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);

                // Subscribe the code to handle requests on this event:
                server.OnExecute += f_server_OnExecute;
                //_server.OnExecute += delegate (IRpcClientInfo client, byte[] bytes) { return new byte[0]; };

                // Start Listening
                server.StartListening();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
        }
示例#2
0
        public void active(string endpoint)
        {
            // The client and server must agree on the interface id to use:
            var iid = new Guid("{78323803-786f-4f7b-908d-b2e89c41d45f}");

            // Create the server instance, adjust the defaults to your needs.
            source = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false);

            try
            {
                // Add an endpoint so the client can connect, this is local-host only:
                source.AddProtocol(RpcProtseq.ncalrpc, endpoint, 100);

                // If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true
                // server.AddProtocol(RpcProtseq.ncacn_ip_tcp, @"8080", 25);

                // Add the types of authentication we will accept
                source.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE);
                source.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                source.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);

                source.OnExecute += executeDispatch;

                source.OnExecuteAsync += executeAsyncDispatch;

                source.StartListening();
            }
            catch (Exception e)
            {
                throw;
            }
        }
        private static void Main()
        {
            // The client and server must agree on the interface id to use:
            var iid = new Guid("{1B617C4B-BF68-4B8C-AE2B-A77E6A3ECEC5}");

            // Create the server instance, adjust the defaults to your needs.
            using (var server = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false))
            {
                try
                {
                    // Add an endpoint so the client can connect, this is local-host only:
                    server.AddProtocol(RpcProtseq.ncalrpc, "RpcExampleClientServer", 100);

                    // If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true
                    // server.AddProtocol(RpcProtseq.ncacn_ip_tcp, @"8080", 25);

                    // Add the types of authentication we will accept
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);

                    // Subscribe the code to handle requests on this event:
                    server.OnExecute += (IRpcClientInfo client, byte[] bytes) =>
                    {
                        //Impersonate the caller:
                        using (client.Impersonate())
                        {
                            var reqBody = Encoding.UTF8.GetString(bytes);
                            Console.WriteLine("Received '{0}' from {1}", reqBody, client.ClientUser.Name);

                            return(Encoding.UTF8.GetBytes(
                                       string.Format(
                                           "Hello {0}, I received your message '{1}'.",
                                           client.ClientUser.Name,
                                           reqBody
                                           )
                                       ));
                        }
                    };

                    // Start Listening
                    server.StartListening();
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Error.WriteLine(ex);
                    Console.ResetColor();
                }

                // Wait until you are done...
                Console.WriteLine("Server is listening. Press [Enter] to exit.");
                Console.ReadLine();
            }
        }
        public void TestPerformanceOnLocalRpc()
        {
            Guid iid = Guid.NewGuid();
            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                    { return arg; };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest"))
                {
                    client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);

                    byte[] bytes = new byte[512];
                    new Random().NextBytes(bytes);

                    Stopwatch timer = new Stopwatch();
                    timer.Start();

                    for (int i = 0; i < 10000; i++)
                        client.Execute(bytes);

                    timer.Stop();
                    Trace.WriteLine(timer.ElapsedMilliseconds.ToString(), "ncalrpc-timming");
                }
            }
        }
        public void TestClientAbandon()
        {
            var iid = Guid.NewGuid();

            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                { return(arg); };

                {
                    RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest");
                    client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);
                    client = null;
                }

                GC.Collect(0, GCCollectionMode.Forced);
                GC.WaitForPendingFinalizers();

                server.StopListening();
            }
        }
示例#6
0
        public void TestUnregisterListener()
        {
            Guid iid = Guid.NewGuid();
            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                RpcServerApi.RpcExecuteHandler handler = 
                    delegate(IRpcClientInfo client, byte[] arg)
                    { return arg; };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest"))
                {
                    client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);

                    server.OnExecute += handler;
                    client.Execute(new byte[0]);
                    
                    server.OnExecute -= handler;
                    try 
                    {
                        client.Execute(new byte[0]);
                        Assert.Fail();
                    }
                    catch (RpcException)
                    { }
                }
            }
        }
示例#7
0
        public void TestPerformanceOnLocalRpc()
        {
            Guid iid = Guid.NewGuid();

            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                { return(arg); };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest"))
                {
                    client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);

                    byte[] bytes = new byte[512];
                    new Random().NextBytes(bytes);

                    Stopwatch timer = new Stopwatch();
                    timer.Start();

                    for (int i = 0; i < 10000; i++)
                    {
                        client.Execute(bytes);
                    }

                    timer.Stop();
                    Trace.WriteLine(timer.ElapsedMilliseconds.ToString(), "ncalrpc-timming");
                }
            }
        }
        public void TestNestedClientImpersonate()
        {
            Guid iid = Guid.NewGuid();

            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                {
                    Assert.AreEqual(false, client.IsImpersonating);
                    using (client.Impersonate())
                    {
                        Assert.AreEqual(true, client.IsImpersonating);
                        using (client.Impersonate())
                            Assert.AreEqual(true, client.IsImpersonating);
                        //does not dispose, we are still impersonating
                        Assert.AreEqual(true, client.IsImpersonating);
                    }
                    Assert.AreEqual(false, client.IsImpersonating);
                    return(arg);
                };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename"))
                {
                    client.AuthenticateAs(RpcClientApi.Self);
                    client.Execute(new byte[0]);
                }
            }
        }
        public void TestUnregisterListener()
        {
            Guid iid = Guid.NewGuid();

            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                RpcServerApi.RpcExecuteHandler handler =
                    delegate(IRpcClientInfo client, byte[] arg)
                { return(arg); };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest"))
                {
                    client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);

                    server.OnExecute += handler;
                    client.Execute(new byte[0]);

                    server.OnExecute -= handler;
                    try
                    {
                        client.Execute(new byte[0]);
                        Assert.Fail();
                    }
                    catch (RpcException)
                    { }
                }
            }
        }
示例#10
0
        public void TestClientAbandon()
        {
            Guid iid = Guid.NewGuid();
            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                    { return arg; };

                {
                    RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "lrpctest");
                    client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);
                    client = null;
                }

                GC.Collect(0, GCCollectionMode.Forced);
                GC.WaitForPendingFinalizers();

                server.StopListening();
            }
        }
        public void TestClientOnNamedPipe()
        {
            Guid iid = Guid.NewGuid();
            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                    {
                        Assert.AreEqual(0, arg.Length);
                        Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_WINNT, client.AuthenticationLevel);
                        Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, client.ProtectionLevel);
                        Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType);
                        Assert.AreEqual(new byte[0], client.ClientAddress);
                        Assert.AreEqual(0, client.ClientPid.ToInt32());
                        Assert.AreEqual(String.Empty, client.ClientPrincipalName);
                        Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name, client.ClientUser.Name);
                        Assert.AreEqual(true, client.IsClientLocal);
                        Assert.AreEqual(true, client.IsAuthenticated);
                        Assert.AreEqual(false, client.IsImpersonating);
                        using (client.Impersonate())
                            Assert.AreEqual(true, client.IsImpersonating);
                        Assert.AreEqual(false, client.IsImpersonating);
                        return arg;
                    };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename"))
                {
                    client.AuthenticateAs(RpcClientApi.Self);
                    client.Execute(new byte[0]);
                }
            }
        }
示例#12
0
        private void ServerLoop()
        {
            Guid iid = new Guid(Constants.ACWatchDogInteropId);

            using (RpcServerApi server = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false))
            {
                // Add an endpoint so the client can connect, this is local-host only:
                server.AddProtocol(RpcProtseq.ncalrpc, Constants.ACWatchDogInteropEndpoint, 100);
                // Add the types of authentication we will accept
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE);
                // Subscribe the code to handle requests on this event:
                server.OnExecute += Server_OnExecute;
                // Start Listening
                server.StartListening();
                @continue = true;
                while (@continue)
                {
                    Thread.Sleep(100);
                }
                server.StopListening();
            }
        }
        public void TestClientOnAnonymousPipe()
        {
            Guid iid = Guid.NewGuid();

            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                {
                    Assert.AreEqual(0, arg.Length);
                    Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_NONE, client.AuthenticationLevel);
                    Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE, client.ProtectionLevel);
                    Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType);
                    Assert.AreEqual(new byte[0], client.ClientAddress);
                    Assert.AreEqual(0, client.ClientPid.ToInt32());
                    Assert.AreEqual(null, client.ClientPrincipalName);
                    Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetAnonymous().Name, client.ClientUser.Name);
                    Assert.AreEqual(true, client.IsClientLocal);
                    Assert.AreEqual(false, client.IsAuthenticated);
                    Assert.AreEqual(false, client.IsImpersonating);

                    bool failed = false;
                    try { client.Impersonate().Dispose(); }
                    catch (UnauthorizedAccessException) { failed = true; }
                    Assert.AreEqual(true, failed);
                    return(arg);
                };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename"))
                {
                    client.AuthenticateAs(RpcClientApi.Anonymous);
                    client.Execute(new byte[0]);
                }
            }
        }
        static void ReversePingTest(RpcProtseq protocol, string[] hostNames, string endpoint, RpcAuthentication auth)
        {
            Guid iid = Guid.NewGuid();

            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                {
                    Array.Reverse(arg);
                    return(arg);
                };

                server.AddProtocol(protocol, endpoint, 5);
                server.AddAuthentication(auth);
                server.StartListening();

                byte[] input  = Encoding.ASCII.GetBytes("abc");
                byte[] expect = Encoding.ASCII.GetBytes("cba");

                foreach (string hostName in hostNames)
                {
                    using (RpcClientApi client = new RpcClientApi(iid, protocol, hostName, endpoint))
                    {
                        client.AuthenticateAs(null, auth == RpcAuthentication.RPC_C_AUTHN_NONE
                                                      ? RpcClientApi.Anonymous
                                                      : RpcClientApi.Self,
                                              auth == RpcAuthentication.RPC_C_AUTHN_NONE
                                                      ? RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE
                                                      : RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY,
                                              auth);

                        Assert.AreEqual(expect, client.Execute(input));
                    }
                }
            }
        }
示例#15
0
        static void ReversePingTest(RpcProtseq protocol, string[] hostNames, string endpoint, RpcAuthentication auth)
        {
            Guid iid = Guid.NewGuid();
            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.OnExecute += 
                    delegate(IRpcClientInfo client, byte[] arg)
                    {
                        Array.Reverse(arg);
                        return arg;
                    };

                server.AddProtocol(protocol, endpoint, 5);
                server.AddAuthentication(auth);
                server.StartListening();

                byte[] input = Encoding.ASCII.GetBytes("abc");
                byte[] expect = Encoding.ASCII.GetBytes("cba");

                foreach (string hostName in hostNames)
                {
                    using (RpcClientApi client = new RpcClientApi(iid, protocol, hostName, endpoint))
                    {
                        client.AuthenticateAs(null, auth == RpcAuthentication.RPC_C_AUTHN_NONE
                                                      ? RpcClientApi.Anonymous
                                                      : RpcClientApi.Self, 
                                                  auth == RpcAuthentication.RPC_C_AUTHN_NONE
                                                      ? RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE
                                                      : RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY,
                                                  auth);

                        Assert.AreEqual(expect, client.Execute(input));
                    }
                }
            }
        }
        public void TestClientOnNamedPipe()
        {
            Guid iid = Guid.NewGuid();

            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                {
                    Assert.AreEqual(0, arg.Length);
                    Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_WINNT, client.AuthenticationLevel);
                    Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, client.ProtectionLevel);
                    Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType);
                    Assert.AreEqual(new byte[0], client.ClientAddress);
                    Assert.AreEqual(0, client.ClientPid.ToInt32());
                    Assert.AreEqual(String.Empty, client.ClientPrincipalName);
                    Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name, client.ClientUser.Name);
                    Assert.AreEqual(true, client.IsClientLocal);
                    Assert.AreEqual(true, client.IsAuthenticated);
                    Assert.AreEqual(false, client.IsImpersonating);
                    using (client.Impersonate())
                        Assert.AreEqual(true, client.IsImpersonating);
                    Assert.AreEqual(false, client.IsImpersonating);
                    return(arg);
                };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename"))
                {
                    client.AuthenticateAs(RpcClientApi.Self);
                    client.Execute(new byte[0]);
                }
            }
        }
示例#17
0
        static void Main(string[] args)
        {
            // The client and server must agree on the interface id to use:
            var iid = new Guid("{f4db45dc-0dcb-4003-b680-56c40f6cb6a8}");

            // Create the server instance, adjust the defaults to your needs.
            using (var server = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false))
            {
                try
                {
                    // Add an endpoint so the client can connect, this is local-host only:
                    server.AddProtocol(RpcProtseq.ncalrpc, "1234", 100);

                    // If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true
                    // server.AddProtocol(RpcProtseq.ncacn_ip_tcp, @"8080", 25);

                    // Add the types of authentication we will accept
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);

                    // Subscribe the code to handle requests on this event:
                    server.OnExecute +=
                        delegate(IRpcClientInfo client, byte[] bytes)
                    {
                        //Impersonate the caller:
                        using (client.Impersonate())
                        {
                            var reqBody = Encoding.UTF8.GetString(bytes);
                            Console.WriteLine("Received '{0}' from {1}", reqBody, client.ClientUser.Name);

                            return(Encoding.UTF8.GetBytes(
                                       String.Format(
                                           "Hello {0}, I received your message '{1}'.",
                                           client.ClientUser.Name,
                                           reqBody
                                           )
                                       ));
                        }
                    };

                    server.OnExecuteAsync +=
                        delegate(IRpcClientInfo client, IntPtr pAsyncState, byte[] bytes)
                    {
                        //Impersonate the caller:
                        using (client.Impersonate())
                        {
                            var reqBody = Encoding.UTF8.GetString(bytes);
                            Console.Write("Received (async) '{0}' from {1} ...", reqBody, client.ClientUser.Name);
                            Thread.Sleep(3 * 1000);
                            Console.WriteLine("Wake up!");
                            return(Encoding.UTF8.GetBytes(
                                       String.Format(
                                           "Hello {0}, I received your message '{1}'.",
                                           client.ClientUser.Name,
                                           reqBody
                                           )
                                       ));
                        }
                    };


                    // Start Listening
                    server.StartListening();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex);
                }

                // Wait until you are done...
                Console.WriteLine("Press [Enter] to exit...");
                Console.ReadLine();
            }
        }
示例#18
0
        static void Main(string[] args)
        {
            // The client and server must agree on the interface id to use:
            //var iid = new Guid("{0092F74D-0EA7-4667-A89F-A04C64244031}");
            var iid = new Guid("{78323803-786f-4f7b-908d-b2e89c41d45f}");

            // Create the server instance, adjust the defaults to your needs.
            using (var server = new RpcServerApi(iid, 100, ushort.MaxValue, allowAnonTcp: false))
            {
                try
                {
                    // Add an endpoint so the client can connect, this is local-host only:
                    server.AddProtocol(RpcProtseq.ncalrpc, "12345", 100);

                    // If you want to use TCP/IP uncomment the following, make sure your client authenticates or allowAnonTcp is true
                    // server.AddProtocol(RpcProtseq.ncacn_ip_tcp, @"8080", 25);

                    // Add the types of authentication we will accept
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_GSS_NEGOTIATE);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                    server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);

                    // Subscribe the code to handle requests on this event:
                    server.OnExecute +=
                        delegate(IRpcClientInfo client, byte[] bytes)
                    {
                        //Impersonate the caller:
                        using (client.Impersonate())
                        {
                            Console.WriteLine("Received'{0}' from {1} ...", bytes.Length, client.ClientUser.Name);
                            byte[] response;

                            var memoryStream = new MemoryStream(bytes);
                            RpcProto.Request message;
                            try
                            {
                                message = ProtoBuf.Serializer.Deserialize <RpcProto.Request>(memoryStream);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw e;
                            }

                            switch (message.method)
                            {
                            case RpcProto.RequestType.GetCustomers:
                                var r = message.post;
                                Console.WriteLine("Received '{0}'", r);
                                var rpn = new RpcProto.GetCustomerResponse()
                                {
                                    result = 0,
                                };
                                response = toBytes(rpn);
                                break;


                            default:
                                response = new byte[0];
                                break;
                            }

                            return(response);
                        }
                    };

                    server.OnExecuteAsync +=
                        delegate(IRpcClientInfo client, IntPtr pAsyncState, byte[] bytes)
                    {
                        //Impersonate the caller:
                        using (client.Impersonate())
                        {
                            Console.WriteLine("Received'{0}' from {1} ...", bytes.Length, client.ClientUser.Name);

                            byte[] response;

                            var memoryStream = new MemoryStream(bytes);
                            RpcProto.Request message;
                            try
                            {
                                message = ProtoBuf.Serializer.Deserialize <RpcProto.Request>(memoryStream);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw e;
                            }

                            switch (message.method)
                            {
                            case RpcProto.RequestType.PostCustomer:
                                var r1 = message.post;
                                Console.WriteLine("Received '{0}'", r1);
                                var rpn1 = new RpcProto.PostCustomerResponse()
                                {
                                    result = 0
                                };
                                response = toBytes(rpn1);
                                break;

                            case RpcProto.RequestType.DeleteCustomer: { }
                                var r2 = message.delete;
                                Console.WriteLine("Received '{0}'", r2);
                                var rpn2 = new RpcProto.DeleteCustomerResponse()
                                {
                                    result = 0
                                };
                                response = toBytes(rpn2);
                                break;

                            default:
                                response = new byte[0];
                                break;
                            }

                            return(response);
                        }
                    };

                    // Start Listening
                    server.StartListening();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex);
                }

                // Wait until you are done...
                Console.WriteLine("Press [Enter] to exit...");
                Console.ReadLine();
            }
        }
        public void TestClientOnAnonymousPipe()
        {
            Guid iid = Guid.NewGuid();
            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_NONE);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                    {
                        Assert.AreEqual(0, arg.Length);
                        Assert.AreEqual(RpcAuthentication.RPC_C_AUTHN_NONE, client.AuthenticationLevel);
                        Assert.AreEqual(RpcProtectionLevel.RPC_C_PROTECT_LEVEL_NONE, client.ProtectionLevel);
                        Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType);
                        Assert.AreEqual(new byte[0], client.ClientAddress);
                        Assert.AreEqual(0, client.ClientPid.ToInt32());
                        Assert.AreEqual(null, client.ClientPrincipalName);
                        Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetAnonymous().Name, client.ClientUser.Name);
                        Assert.AreEqual(true, client.IsClientLocal);
                        Assert.AreEqual(false, client.IsAuthenticated);
                        Assert.AreEqual(false, client.IsImpersonating);

                        bool failed = false;
                        try { client.Impersonate().Dispose(); }
                        catch (UnauthorizedAccessException) { failed = true; }
                        Assert.AreEqual(true, failed);
                        return arg;
                    };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename"))
                {
                    client.AuthenticateAs(RpcClientApi.Anonymous);
                    client.Execute(new byte[0]);
                }
            }
        }
示例#20
0
 public Win32RpcServer AddAuthentication(RpcAuthentication auth)
 {
     _server.AddAuthentication(auth, ServerPrincipalName);
     return(this);
 }
        public void TestNestedClientImpersonate()
        {
            Guid iid = Guid.NewGuid();
            using (RpcServerApi server = new RpcServerApi(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcClientInfo client, byte[] arg)
                    {
                        Assert.AreEqual(false, client.IsImpersonating);
                        using (client.Impersonate())
                        {
                            Assert.AreEqual(true, client.IsImpersonating);
                            using (client.Impersonate())
                                Assert.AreEqual(true, client.IsImpersonating); 
                            //does not dispose, we are still impersonating
                            Assert.AreEqual(true, client.IsImpersonating);
                        }
                        Assert.AreEqual(false, client.IsImpersonating);
                        return arg;
                    };

                using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncacn_np, null, @"\pipe\testpipename"))
                {
                    client.AuthenticateAs(RpcClientApi.Self);
                    client.Execute(new byte[0]);
                }
            }
        }