【发布时间】:2010-01-20 22:34:40
【问题描述】:
我有一个带有服务器“AppDomain”的应用程序,该服务器接受来自不同 AppDomain(托管插件,由其他人开发且不可信)的调用。
从服务器 AppDomain 中,我需要知道实际上是哪个“插件”(AppDomain)在进行调用,这样我才能确保这个插件可以访问资源。
我可以将凭据传递给远程处理方法调用,但我担心这样做会导致“插件 A”的狡猾程序员可能会更改代码,使其看起来来自“插件 B”。
我研究了在服务器应用程序上创建自己的“ObjRef”实现,认为“ChannelInfo.ChannelData”可能包含有关进行调用的客户端插件的信息,并实现了以下代码:
public int DomainId
{
get
{
int domainId = -1;
// The type "System.Runtime.Remoting.Channels.CrossAppDomainData" is not Public,
// so we have to use reflection to get access to it.
for (int i = 0; i < ChannelInfo.ChannelData.Length; i++)
{
object o = ChannelInfo.ChannelData[i];
if (o.ToString() == "System.Runtime.Remoting.Channels.CrossAppDomainData")
{
System.Reflection.BindingFlags flags =
System.Reflection.BindingFlags.GetProperty
| System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.NonPublic;
domainId = (int)o.GetType().GetProperty("DomainID", flags).GetValue(o, null);
}
}
return domainId;
}
}
但是当我真的想要客户端(调用者)AppDomain Id时,由此检索到的DomainId与Servers AppDomain.CurrentDomain.Id相同
感觉这太难了:-)
有什么想法吗?
【问题讨论】:
标签: reflection remoting appdomain