The remote certificate is invalid according to the validation procedure.

I was adding e-mail code to my ASP.NET Core MVC web app and got this error when trying to send an e-mail using my O365 account.

The remote certificate is invalid according to the validation procedure.

The solution was to add the following routine.

private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    //Console.WriteLine(certificate);
    return true;
}

Then call it from the e-mail code like this.

//Added this line here
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
SmtpClient smtp = new SmtpClient();

So altogether it looks like this.

private void sendAMail(String toAddress, String messageBody)
        {
            String msg = "Sending mail to : " + toAddress;

            MailMessage mail = new MailMessage();
            mail.To.Add(toAddress);
            mail.From = new MailAddress("from@mydomain.com");
            mail.Subject = "Subject: Test Mail";
            mail.Body = messageBody;
            mail.IsBodyHtml = true;            

            //Added this line here
            System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
            SmtpClient smtp = new SmtpClient();

            smtp.Host = "myhostname.com";            
            smtp.Credentials = new System.Net.NetworkCredential("sender@sample.com", "");
            smtp.EnableSsl = true;
            smtp.Port = 587;            
            smtp.Send(mail);            
        }


private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    //Console.WriteLine(certificate);
    return true;
}

I found the solution here: c# - "The remote certificate is invalid according to the validation procedure." using Gmail SMTP server - Stack Overflow

SharePoint Custom Pop-up not posting back after first attempt.

This change was required for the SharePoint 2013 Upgrade (from SharePoint 2010), otherwise the page would only post back once and needed to be closed and re-opened to resubmit updates. The change was in private Control RenderHeaderBar()

var header2 = new HtmlGenericControl("h4");

string text2 = "Report ID:" + InspectionReportId + " GUID:" + InspectionReportGuid;

header2.InnerText = text2;

headerBox.Controls.Add(header2);

Authentication Failed because remote party has closed the transport stream

Found Here: .net - Authentication failed because remote party has closed the transport stream - Stack Overflow

I came across this issue when trying to send an e-mail to O365 via .NET C# code.

 

My Option is add the following Registry key:
Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319 
Value: SchUseStrongCrypto 

It is worth noting that .NET 4.6 will use the correct protocol by default and does not require either solution.

WinForms Tab Order not working

So I just wasted 2 1/2 hours tring to figure out the WHY the Tab ordering was not working on my Form (C# project Visual Studio 2010).

No matter what I did the order DID NOT CHANGE. There are all kinds of controls like Groupboxes, Panels with Controls inside Controls. No big deal. The View -> Tab Order feature made NO DIFFERENCE at all!!

The only way the tab Order worked was by actually updating the DESIGNER to order the Controls in the way I wanted! Like this...

            this.pnlSubAbc.Controls.Add(this.lblProductA);
            this.pnlSubAbc.Controls.Add(this.cboProductA);           
            this.pnlSubAbc.Controls.Add(this.lblProductAH2S);
            this.pnlSubAbc.Controls.Add(this.txtProductAH2S);  
            this.pnlSubAbc.Controls.Add(this.lblProductB);
            this.pnlSubAbc.Controls.Add(this.cboProductB);        
            this.pnlSubAbc.Controls.Add(this.lblProductBH2S);
            this.pnlSubAbc.Controls.Add(this.txtProductBH2S);
            this.pnlSubAbc.Controls.Add(this.lblProductC);
            this.pnlSubAbc.Controls.Add(this.cboProductC);
            this.pnlSubAbc.Controls.Add(this.lblProductCH2S);
            this.pnlSubAbc.Controls.Add(this.txtProductCH2S);
            this.pnlSubAbc.Location = new System.Drawing.Point(3, 344);
            this.pnlSubAbc.Name = "pnlSubAbc";
            this.pnlSubAbc.Size = new System.Drawing.Size(562, 212);
            this.pnlSubAbc.TabIndex = 2;
            this.pnlSubAbc.TabStop = false;
            this.pnlSubAbc.Text = "Products Carried";

Hope this Helps someone else out there.

How to avoid page refresh,flickring in postback

It is very very simple to avoid p page refresh in post-back . To get this we just have to use ASP "Update Panel Control" it will not refresh the page again and again on post-back.


Here is an example.

In Default.aspx



<asp:UpdatePanel ID="pnlPageRefresh" runat="server">
        <ContentTemplate>
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                </asp:DropDownList>
                <br />
                <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True"></asp:TextBox>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
                </asp:RadioButtonList>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
<div>
<asp:Button ID="btnSave" runat="server" Text="Button" />
</div>


After using this your page will not at all refresh again and again on post-back. Your code will work fine but refresh flickering will not occur.

VS 2010 Unable to automatically step into the server

If you need to debug a WCF Server, or somehow need to step into code on IIS, you will need to do the following to prevent the message  “Unable to automatically step into the server. Unable to determine a stopping location”. The message might be slightly different but generally it means you can't debug in IIS!

It turns out that Visual Studio 2010 comes with a default setting to debug ‘Just my code’.

To correct the problem, go to Tools – Options – Debugging – General and switch off the option ‘enable just my code’.  

And of course don’t forget to set debug="true" in the web.config file of the webservice. Double check your publishing Transformations too if using any, as that might be removing the debug="true" property in your web.Config file.

This will hopefully help someone else out there!

Dealing with Blobs in Oracle, SQL and MS Access

I came across a challenge this week. Migrating Blob data from Oracle to SQL Server. I used MS Access for this since I could create an ODBC DSN to each and then have the best of ALL worlds. That is review the data, count the records and write very simple code. The link below is excellent and worked perfectly for what I needed for dealing with the Blobs.  

How To Read and Write BLOBs Using GetChunk and AppendChunk: http://support.microsoft.com/default.aspx?scid=kb;en-us;194975

The process I used for the Blobs was this:
1. Copy the data from Oracle to SQL exluding the Blobs Column.
2. Export the Blobs to File using the ID field as a key like (ID) & FileName. FileName was a column in my Oracle table so that was nice and pretty well needed so you can Identift the MIME (file type like XLS, DOC, PDF).
3. Finally Update the SQL Server table using File to Blob.

I had some other "things" in the code to deal with missing files from the Blob column but that was simple.