public IMessageLite CallMethod(string methodName, ICodedInputStream input, ExtensionRegistry registry) { WindowsIdentity currentUser = WindowsIdentity.GetCurrent(); if (currentUser == null) { throw new ArgumentNullException(); } IClientContext ctx = RpcCallContext.Current.Client; switch (methodName) { case "ncalrpc": { Assert.AreEqual(new byte[0], ctx.ClientAddress); Assert.AreEqual(Process.GetCurrentProcess().Id, ctx.ClientPid); Assert.AreEqual(true, ctx.ClientUser.IsAuthenticated); Assert.IsTrue(ctx.ClientUser.AuthenticationType == "NTLM" || ctx.ClientUser.AuthenticationType == "Negotiate" || ctx.ClientUser.AuthenticationType == "Kerberos"); Assert.AreEqual(currentUser.Name, ctx.ClientUser.Name); Assert.AreEqual(true, ctx.IsClientLocal); Assert.AreEqual(true, ctx.IsAuthenticated); Assert.AreEqual(false, ctx.IsImpersonating); using (ctx.Impersonate()) Assert.AreEqual(true, ctx.IsImpersonating); break; } case "ncacn_ip_tcp": { Assert.AreEqual(16, ctx.ClientAddress.Length); Assert.AreEqual(true, ctx.ClientUser.IsAuthenticated); Assert.IsTrue(ctx.ClientUser.AuthenticationType == "NTLM" || ctx.ClientUser.AuthenticationType == "Negotiate" || ctx.ClientUser.AuthenticationType == "Kerberos"); Assert.AreEqual(currentUser.Name, ctx.ClientUser.Name); Assert.AreEqual(true, ctx.IsAuthenticated); Assert.AreEqual(false, ctx.IsImpersonating); using (ctx.Impersonate()) Assert.AreEqual(true, ctx.IsImpersonating); break; } case "ncacn_np": { Assert.AreEqual(new byte[0], ctx.ClientAddress); Assert.AreEqual(true, ctx.ClientUser.IsAuthenticated); Assert.IsTrue(ctx.ClientUser.AuthenticationType == "NTLM" || ctx.ClientUser.AuthenticationType == "Negotiate" || ctx.ClientUser.AuthenticationType == "Kerberos"); Assert.AreEqual(currentUser.Name, ctx.ClientUser.Name); Assert.AreEqual(true, ctx.IsAuthenticated); Assert.AreEqual(false, ctx.IsImpersonating); using (ctx.Impersonate()) Assert.AreEqual(true, ctx.IsImpersonating); break; } case "ncacn_np-Anonymous": { Assert.AreEqual(new byte[0], ctx.ClientAddress); Assert.AreEqual(false, ctx.ClientUser.IsAuthenticated); Assert.AreEqual("", ctx.ClientUser.AuthenticationType); Assert.AreEqual("", ctx.ClientUser.Name); Assert.AreEqual(false, ctx.IsAuthenticated); Assert.AreEqual(false, ctx.IsImpersonating); try { // impersonation not allowed when no credentials were provided, however, you can use ctx.ClientUser.Impersonate ctx.Impersonate(); } catch (UnauthorizedAccessException) { } Assert.AreEqual(false, ctx.IsImpersonating); break; } } return(RpcVoid.DefaultInstance); }