Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Overwrite InfoProperty of generic wcf client

Hi all
following question:
How do I overwrite a property of a instance of a wcf client which is instantiated as follow:
public abstaract class Manager<T> : IDisposable where T : class, ICommunicationObject, new()
{
    protected T wcfClient;
    public ConnectionManager()
    {
            wcfClient = new T();
    }
}

Open in new window

The reason I'm using the Manager is because I have several services which I use and wanted to centralize dispose and create method...

Now the Manager code is run as user@domain but the wcfClient must authenticate on the server side with otherUser@otherDomain which forces me to do some thing like:

check this: Impersonate a totally different Windows User in a client

Now I wanted to keep the Manager and add something like
Type cType = wcfClient.GetType().getBaseType;

PropertyInfo[] props = cType.GetProperty("ClientCredentials")
							.PropertyType.GetProperty("Windows")
							.PropertyType.GetProperty("ClientCredential")
							.PropertyType.GetProperties();


foreach (PropertyInfo p in props)
{
    if (p.Name.Equals("Domain"))
    {	
	/*
	 * How do I instantiate the the outerprop ?? */
	object outerProp = p.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
        if (p.CanWrite)
        {
            p.SetValue(outerProp, "otherDomain", null);
        }
    }
	
	if (p.Name.Equals("UserName"))
    {	
	/*
	* How do I instantiate the the outerprop ?? */
	object outerProp = p.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
        if (p.CanWrite)
        {
            p.SetValue(outerProp, "username", null);
        }
    }
	
	if (p.Name.Equals("Password"))
    {	
	/*
	* How do I instantiate the the outerprop ?? */
	object outerProp = p.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
        if (p.CanWrite)
        {
            p.SetValue(outerProp, "xxxxxx", null);
        }
    }
}

Open in new window

to add/overwrite the client credential but got stuck.
How do I instantiate the outerProp to correctly use the SetValue method on the specific InfoProperty of the wcfClient.

Or is there a way to get the wcfClient instance knowing the type?

Any better way to reset/overwrite the wcfClient credential could also be a solution to my problem...

Thanks for your support
th*
0
thomasbau65
Asked:
thomasbau65
1 Solution
 
thomasbau65Author Commented:
Well then
As state before "one is never better served as by one self" :-)
Here my solution:

Fist the property I wanted to overwrite is not writable at run time.
Second I don't need to go to the base property using :
PropertyInfo[] props = cType.GetProperty("ClientCredentials")
							.PropertyType.GetProperty("Windows")
							.PropertyType.GetProperty("ClientCredential")
							.PropertyType.GetProperties();

Open in new window

I only have to instantiate the new "ClientCredentials" and try to replace it on the dynamically created object...
ClientCredentials cred = new ClientCredentials();
    cred.Windows.ClientCredential.Domain = "otherDomain";
    cred.Windows.ClientCredential.UserName = "username";
    cred.Windows.ClientCredential.Password = "xxxxxx";
    cred.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

	object oclient = Activator.CreateInstance(wcfClient.GetType());
    PropertyInfo credProp = oclient.GetType().GetProperty("ClientCredentials");
    if (credProp.CanWrite)
    {
        credProp.SetValue(client, cred, null);
    }

Open in new window

But as I bitterly found out the "credProp" is not writable and thus can't be replaced
0

Featured Post

When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now