Thursday, 15 January 2015

c# - Temporary disable certificate validation -



c# - Temporary disable certificate validation -

i need consume web service, has certificate , need disable authentication.

the server side can dummy server using "simulate" results or real 3rd side server utilize secured soap headers

i need able disable certificate validation in server when invoking dummy server, enable when invoking real server (which not ours)

i saw in post way disable this:

servicepointmanager.servercertificatevalidationcallback = delegate { homecoming true; };

which works perfectly.

the problem 1 time line of code executed can't seem "reverse" it

i need like:

if (testmode) { servicepointmanager.servercertificatevalidationcallback = delegate { homecoming true; }; } else { //enable certificate validation }

btw, code in c#.

any ideas ?

you need de-register callback, this:

declare remotecertificatevalidationcallback reference.

static remotecertificatevalidationcallback _callbacktrue;

then in initialization code, assign this:

_callbacktrue = (sender, cert, chain, sslpolicyerrors) => true;

then can register / deregister callback when testmode property changes:

// in setter testmode, when set true. servicepointmanager.servercertificatevalidationcallback += _callbacktrue; // in setter testmode, when set false servicepointmanager.servercertificatevalidationcallback -= _callbacktrue;

just create sure register callback when testmode goes 'false true' , de-register callback when goes 'true false'. i.e. registrations / deregistrations should symmetrical.

c# certificate

No comments:

Post a Comment