alexyap1205
2008-08-27 01:53:00 UTC
Hi,
I am trying out impersonation with IPCChannel but for some reason it always
throws an Access Denied Exception when I tried to connect to the port. Could
anyone help me how to do this?
Here is the code that I've used:
Server:
BinaryServerFormatterSinkProvider serverProvider = new
BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
System.Collections.IDictionary props = new
System.Collections.Hashtable();
props["portName"] = "TestRemoteObject";
props["name"] = "TestRemoteObject";
props["authorizedGroup"] = "Everyone";
props["impersonate"] = true;
props["secure"] = true;
serverChannel = new IpcServerChannel(props, serverProvider);
ChannelServices.RegisterChannel(serverChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
"TestRemoteObject", WellKnownObjectMode.Singleton);
Client:
BinaryClientFormatterSinkProvider clientSink = new
BinaryClientFormatterSinkProvider();
System.Collections.IDictionary clientProps = new
System.Collections.Hashtable();
clientProps["secure"] = true;
clientProps["tokenImpersonationLevel"] =
TokenImpersonationLevel.Delegation;
IpcClientChannel clientChannel = new
IpcClientChannel(clientProps, clientSink);
ChannelServices.RegisterChannel(clientChannel, true);
// Impersonate user to use Administrator
IntPtr token;
bool result = LogonUser(
"Administrator", "MyMachine",
"MyPassword",
LogonTypes.Network,
LogonProviders.Default,
out token);
if (result)
{
WindowsIdentity identity = new WindowsIdentity(token);
using (WindowsImpersonationContext imp =
identity.Impersonate())
{
try
{
RemoteObject remote =
(RemoteObject)Activator.GetObject(typeof(RemoteObject),
"ipc://TestRemoteObject/TestRemoteObject");
message = remote.Echo("Test");
}
catch (Exception exc)
{
message = exc.ToString();
}
imp.Undo();
}
MessageBox.Show(message);
CloseHandle(token);
}
Error Message:
System.Runtime.Remoting.RemotingException: Failed to connect to an IPC Port:
Access is denied.
Server stack trace:
at System.Runtime.Remoting.Channels.Ipc.IpcPort.Connect(String portName,
Boolean secure, TokenImpersonationLevel impersonationLevel, Int32 timeout)
at
System.Runtime.Remoting.Channels.Ipc.ConnectionCache.GetConnection(String
portName, Boolean secure, TokenImpersonationLevel level, Int32 timeout)
at
System.Runtime.Remoting.Channels.Ipc.IpcClientTransportSink.ProcessMessage(IMessage
msg, ITransportHeaders requestHeaders, Stream requestStream,
ITransportHeaders& responseHeaders, Stream& responseStream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg)
Ive tried different values for the tokenImpersonationLevel, but it still has
the same result.
Any advise?
Thank you.
I am trying out impersonation with IPCChannel but for some reason it always
throws an Access Denied Exception when I tried to connect to the port. Could
anyone help me how to do this?
Here is the code that I've used:
Server:
BinaryServerFormatterSinkProvider serverProvider = new
BinaryServerFormatterSinkProvider();
serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
System.Collections.IDictionary props = new
System.Collections.Hashtable();
props["portName"] = "TestRemoteObject";
props["name"] = "TestRemoteObject";
props["authorizedGroup"] = "Everyone";
props["impersonate"] = true;
props["secure"] = true;
serverChannel = new IpcServerChannel(props, serverProvider);
ChannelServices.RegisterChannel(serverChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
"TestRemoteObject", WellKnownObjectMode.Singleton);
Client:
BinaryClientFormatterSinkProvider clientSink = new
BinaryClientFormatterSinkProvider();
System.Collections.IDictionary clientProps = new
System.Collections.Hashtable();
clientProps["secure"] = true;
clientProps["tokenImpersonationLevel"] =
TokenImpersonationLevel.Delegation;
IpcClientChannel clientChannel = new
IpcClientChannel(clientProps, clientSink);
ChannelServices.RegisterChannel(clientChannel, true);
// Impersonate user to use Administrator
IntPtr token;
bool result = LogonUser(
"Administrator", "MyMachine",
"MyPassword",
LogonTypes.Network,
LogonProviders.Default,
out token);
if (result)
{
WindowsIdentity identity = new WindowsIdentity(token);
using (WindowsImpersonationContext imp =
identity.Impersonate())
{
try
{
RemoteObject remote =
(RemoteObject)Activator.GetObject(typeof(RemoteObject),
"ipc://TestRemoteObject/TestRemoteObject");
message = remote.Echo("Test");
}
catch (Exception exc)
{
message = exc.ToString();
}
imp.Undo();
}
MessageBox.Show(message);
CloseHandle(token);
}
Error Message:
System.Runtime.Remoting.RemotingException: Failed to connect to an IPC Port:
Access is denied.
Server stack trace:
at System.Runtime.Remoting.Channels.Ipc.IpcPort.Connect(String portName,
Boolean secure, TokenImpersonationLevel impersonationLevel, Int32 timeout)
at
System.Runtime.Remoting.Channels.Ipc.ConnectionCache.GetConnection(String
portName, Boolean secure, TokenImpersonationLevel level, Int32 timeout)
at
System.Runtime.Remoting.Channels.Ipc.IpcClientTransportSink.ProcessMessage(IMessage
msg, ITransportHeaders requestHeaders, Stream requestStream,
ITransportHeaders& responseHeaders, Stream& responseStream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg)
Ive tried different values for the tokenImpersonationLevel, but it still has
the same result.
Any advise?
Thank you.
--
Alexander Yap
Alexander Yap