1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
[InterceptorProxy]
public class ContextBoundController : ContextBoundObject
{
public ContextBoundController()
: base()
{
}
}
public class InterceptorProxy : RealProxy
{
[System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
private MarshalByRefObject m_realObject;
public InterceptorProxy()
{
}
public InterceptorProxy(Type classToProxy)
: base(classToProxy)
{
}
//private Dictionary<object, PrincipalPermissionAttribute> _securedMethods = new Dictionary<object,PrincipalPermissionAttribute>();
//public Dictionary<object, PrincipalPermissionAttribute> SecuredMethods
//{
// get { return _securedMethods; }
// set { _securedMethods = value; }
//}
public override IMessage Invoke(IMessage msg)
{
IMessage returnMessage = null;
if (msg is IConstructionCallMessage)
{
IConstructionCallMessage ctorCallMessage = msg as IConstructionCallMessage;
returnMessage = InitializeServerObject(ctorCallMessage);
m_realObject = GetUnwrappedServer();
SetStubData(this, m_realObject);
}
else if (msg is IMethodCallMessage)
{
IMethodCallMessage methodCallMessage = null;
IMethodReturnMessage rawReturnMessage = null;
methodCallMessage = msg as IMethodCallMessage;
Preprocess(methodCallMessage);
rawReturnMessage = RemotingServices.ExecuteMessage(m_realObject, methodCallMessage);
if ((rawReturnMessage.Exception != null) && (rawReturnMessage.Exception.GetType()== typeof(SecurityException)))
{
DialogResult result = DialogResult.None;
CredentialsDialog credentialsDialog = new CredentialsDialog("", "... authentication.");
credentialsDialog.SaveDisplayed = false;
credentialsDialog.Message = "Welcome to ...";
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
IntPtr tokenHandle = new IntPtr(0);
bool logged = false;
do
{
result = credentialsDialog.Show();
logged = LogonUser(credentialsDialog.Name, "domain.local", credentialsDialog.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
if (logged)
{
WindowsIdentity identity = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext context = identity.Impersonate();
IPrincipal oldPrincipal = Thread.CurrentPrincipal;
Thread.CurrentPrincipal = new WindowsPrincipal(identity);
//Recall with impersonate
rawReturnMessage = RemotingServices.ExecuteMessage(m_realObject, methodCallMessage);
context.Undo();
Thread.CurrentPrincipal = oldPrincipal;
}
}
while ((result == DialogResult.OK && logged == false) || (logged && rawReturnMessage.Exception != null && rawReturnMessage.Exception.GetType() == typeof(SecurityException)));
MessageBox.Show(logged.ToString());
if (!logged)
throw rawReturnMessage.Exception;
}
returnMessage = PostProcess(methodCallMessage, rawReturnMessage);
}
else
{
throw new NotSupportedException();
}
return returnMessage;
}
private void Preprocess(IMessage msg)
{
//if (SecuredMethods.ContainsKey(msg.Properties["__MethodName"]))
//{
// PrincipalPermissionAttribute attr = SecuredMethods[msg.Properties["__MethodName"]];
// try
// {
// attr.CreatePermission().Demand();
// }
// catch (SecurityException ex)
// {
// throw;
// }
//}
// Do interception work here.
}
private IMessage PostProcess(IMessage msg, IMessage msgReturn)
{
// Do interception work here.
return msgReturn;
}
}
[AttributeUsage(AttributeTargets.Class)]
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
public class InterceptorProxyAttribute : ProxyAttribute
{
public InterceptorProxyAttribute()
{
}
public override MarshalByRefObject CreateInstance(Type serverType)
{
RealProxy proxy = new InterceptorProxy(serverType);
MarshalByRefObject transparentProxy = (MarshalByRefObject)proxy.GetTransparentProxy();
return transparentProxy;
}
} |
Partager