Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ServicePointManager.CertificatePolicy - Trusting all SSL certificates.

Posted on 2007-08-02
7
Medium Priority
?
5,563 Views
Last Modified: 2013-11-07
Morning guys,

I have some old legacy VB code that I am trying to do two things with.
1) I need to migrate it to C# and
2) I want to build it into a class that I can call when needed.

Here is the old VB code:
----------------------------------------------------------------------------------------------------------------------------
Public Class TrustAllCertificatePolicy
    Implements ICertificatePolicy
    Public TrustAllCertificatePolicy()

    Public Function CheckValidationResult(ByVal srvPoint As ServicePoint, _
    ByVal cert As X509Certificate, ByVal request As WebRequest, ByVal problem As Integer) _
    As Boolean Implements ICertificatePolicy.CheckValidationResult

        Return True

    End Function

End Class
&the call to the class
System.Net.ServicePointManager.CertificatePolicy = New TrustAllCertificatePolicy
----------------------------------------------------------------------------------------------------------------------------

This is to gain access to our web service that has a SSL certificate installed. Also the class above was part of the code behind of the page. I want to remove it and place it in a new class to be used on all pages.

I realize the following is wrong, but this is what I have so far???!??!?!

-----------------------------------------------------------------------------------------------------------------------------
namespace Util
{
    public class TrustAllCert : ICertificatePolicy
    {
        public TrustAllCert()
        {

        }

        public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate cert, WebRequest request, int problem)
        {
            return true;
        }
    }
}
...the call to the class
Util.TrustAllCert TrustedCert = new Util.TrustAllCert();
System.Net.ServicePointManager.CertificatePolicy = TrustedCert.CheckValidationResult();

-----------------------------------------------------------------------------------------------------------------------------

How do I accomplish what is being done in the VB code above?

Nugs
0
Comment
Question by:Nugs
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19618720
I would think that you need to do this:

System.Net.ServicePointManager.CertificatePolicy = new Util.TrustAllCert();

Bob
0
 
LVL 2

Author Comment

by:Nugs
ID: 19618756
System.Net.ServicePointManager.ServerCertificateValidationCallback = new Util.TrustAllCert();

i get a error:

Cannot implicitly convert type 'Util.TrustAllCert' to 'System.Net.Security.RemoteCertificateValidationCallback'

ServicePointManager.CertificatePolicy is obsolite in 2.0.
0
 
LVL 2

Author Comment

by:Nugs
ID: 19618769
sorry, posted that before i wanted to:

System.Net.ServicePointManager.ServerCertificateValidationCallback = new Util.TrustAllCert();

i get a error:

Cannot implicitly convert type 'Util.TrustAllCert' to 'System.Net.Security.RemoteCertificateValidationCallback'

ServicePointManager.CertificatePolicy is obsolite in 2.0.

I am not sure if my class is accuratly convert over from the VB version. I think this is half my issue.

Nugs
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19618970
Try this:

using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnValidationCallback);

        public bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

Bob
0
 
LVL 2

Author Comment

by:Nugs
ID: 19619170
ok, cool... So would this work?

default.aspx.cs
-----------------------------------------------------------------------------------------------------------------------
...
Util.TrustAllCert ValCallback = new Util.TrustAllCert();
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValCallback.OnValidationCallback);
...
-----------------------------------------------------------------------------------------------------------------------

TrustAllCert.cs
-----------------------------------------------------------------------------------------------------------------------
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace Util
{
    public class TrustAllCert
    {
        public TrustAllCert()
        {

        }

        public bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }
}
-----------------------------------------------------------------------------------------------------------------------


So my aspx.cs code behind can call the class for the validation callback?

Nugs
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19619269
Try it, and you tell me.

Bob
0
 
LVL 2

Author Comment

by:Nugs
ID: 19619391
:)

Just did, and it does... thanks

Nugs
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question