Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

02 October 2020

How to enable HTTPS access for WCF RESTful Service - WCF .svc 404 not found

In one of my projects, I have developed a WCF service and hosted it on Azure. I was required to create the service RESTful and call it from inside a SharePoint online app; for which, I added the required service configuration in the web.config file.

For creating the RESTful service, we need to use WebHttpBinding for specifying service endpoint and define the endpoint behavior. Here’s the configuration that I have added:

    
        
        
    



    
        
    

Adding this allows accessing my service using RESTful API calls over HTTP access. But, when I tried calling my service from inside the SharePoint online app, it showed the below error:

“The app… was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint…. The request has been blocked; the content must be served over HTTPS."

And then I knew that the service should be secure to be accessible over HTTPS. To secure the custom domain name with HTTPS, it requires binding a custom SSL certificate to the custom domain in Azure.

This can also be done through simple configuration changes in the web.config file. To make the RESTful service accessible over HTTPS, I added the following configuration:

First, it is needed to add a WebHttpBinding configuration with security mode set to ‘Transport’ as below:

    
        
        
    



    
        
            
            
        
    

And then, it required assigning this WebHttpBinding configuration to Service Endpoint binding with httpsGetEnabled set to ‘true’


    
        
                     
        
    

    
        
        
        
    


After adding these configuration settings, I was able to call my service from inside the SharePoint online app using HTTPS access.

With the above configuration, the service will be accessible over HTTP and HTTPS both. If you want to disable HTTP access, and allow the service accessible with HTTPS only, then you can set httpGetEnabled to ‘false’ in the ServicerBehavior settings.



Via https://www.advaiya.com/blog/how-to-enable-https-access-for-wcf-restful-service/

22 November 2017

Download file over HTTPS using Net.WebClient / Scaricare un file via HTTPS con Net.WebClient

Using Net.WebClient over HTTPS returns this error:
The underlying connection was closed: An unexpected error occurred on a send.
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

You have to setup a ServerCertificateValidationCallback event and set the right SecurityProtocol:
Imports System.Net
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates

Public Class HTTPS_Test

    Private Function validateCertificate(sender As Object,
                                         certificate As X509Certificate,
                                         chain As X509Chain,
                                         sslPolicyErrors As SslPolicyErrors
                                         ) As Boolean

        '' If the certificate is a valid, signed certificate, return true.
        'If sslPolicyErrors = Security.SslPolicyErrors.None Then
        '    Return True
        'Else
        '    Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'",
        '                      certificate.Subject,
        '                      sslPolicyErrors.ToString)
        '    Return False
        'End If

        Return True

    End Function


    Public Sub DownloadFromHTTPS()


        '-- IMPOSTAZIONI PER USARE HTTPS/CERTIFICATI - da impostare prima di usare il WebClient
        ServicePointManager.ServerCertificateValidationCallback = AddressOf validateCertificate

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 Or
                                                    SecurityProtocolType.Tls Or
                                                    SecurityProtocolType.Tls11 Or
                                                    SecurityProtocolType.Tls12



        Dim url As String = "https://..."

        Using myWebClient As New WebClient()
            Dim data As String = myWebClient.DownloadString(url)
        End Using

    End Sub

End Class


Docs:
  1. Download file over HTTPS using .Net
  2. Set the SecurityProtocol Ssl3 or Tls on the Net.
  3. Best practices for using ServerCertificateValidationCallback

10 November 2017

IIS: Could not find a base address "WebHttpBinding / BasicHttpBinding" error

IIS on Windows Server 2012 R2 64bit throws this error:
Could not find a base address that matches scheme http for the endpoint with binding BasicHttpBinding. Registered base address schemes are [https].
Impossibile trovare un indirizzo di base corrispondente allo schema http per l'endpoint con binding WebHttpBinding. Gli schemi degli indirizzi di base registrati sono [].

Find and remove from web.config(?):

    

08 January 2015

Pure HTML redirect

<html>
<head>
 <meta http-equiv="refresh" content="0;URL='http://www.mdwiki.info/'" />   
</head>

<body>
 Redirecting to <a href="http://www.mdwiki.info/">http://www.mdwiki.info</a>...
</body>
</html>


Trovato qui.

08 March 2013

Generating UTF-8 with System.Xml.XmlWriter

Today i decided to experiment with XmlWriter. The first i wanted to do was set the Encoding to UTF-8.:
StringBuilder stringBuilder = new StringBuilder();
XmlWriter xmlWriter = XmlWriter.Create(stringBuilder);
xmlWriter.Settings.Encoding = Encoding.UTF8;

When i ran this code i recieved the following exception: XmlException was unhandled: The "XmlWriterSettings.Encoding" property is read only and cannot be set. The documentation for the Settings property clearly says:
The XmlWriterSettings object returned by the Settings property cannot be modified. Any attempt to change individual settings results in an exception being thrown.

So i wrote the following:
StringBuilder stringBuilder = new StringBuilder();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = Encoding.UTF8;
 
XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("root", "http://www.timvw.be/ns");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
 
string xmlString = stringBuilder.ToString();

As you can see: is still not what i want. Apparently is the Encoding property ignored if the XmlWriter is not using a Stream. So here is my next attempt:
MemoryStream memoryStream = new MemoryStream();
// initialize xmlWriterSettings as above...
 
XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
// call the same operations on the xmlWriter as above...
 
string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());

Ok, i'm getting close:
?




Luckily enough i knew that the ? (byte with value 239) at the beginning is the BOM (Byte Order Mark). In order to get rid of that byte i had to create my own instance of UTF8Encoding. Finally, i can present some working code:
MemoryStream memoryStream = new MemoryStream();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UTF8Encoding(false);
xmlWriterSettings.ConformanceLevel = ConformanceLevel.Document;
xmlWriterSettings.Indent = true;
 
XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("root", "http://www.timvw.be/ns");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
 
string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());


Trovato qui.

06 March 2013

Get NBA scores RSS from ESPN

Ad esempio, l'URL http://sports.espn.go.com/nba/bottomline/scores ritorna questa stringa:

&nba_s_delay=120&nba_s_stamp=0306085718&nba_s_left1=^Boston%20109%20%20%20Philadelphia%20101%20(FINAL)&nba_s_right1_1=P.%20Pierce%2018pts,%207ast,%2011reb&nba_s_right1_2=J.%20Holiday%2018pts,%2010ast,%205reb&nba_s_right1_count=2&nba_s_url1=http://sports.espn.go.com/nba/boxscore?gameId=400278616&nba_s_left2=LA%20Lakers%20105%20%20%20^Oklahoma%20City%20122%20(FINAL)&nba_s_right2_1=K.%20Bryant%2030pts,%202ast,%203reb&nba_s_right2_2=R.%20Westbrook%2037pts,%205ast,%2010reb&nba_s_right2_count=2&nba_s_url2=http://sports.espn.go.com/nba/boxscore?gameId=400278617&nba_s_left3=^Denver%20120%20%20%20Sacramento%20113%20(FINAL)&nba_s_right3_1=T.%20Lawson%2024pts,%207ast,%203reb&nba_s_right3_2=I.%20Thomas%2023pts,%208ast,%203reb&nba_s_right3_3=D.%20Cousins%205%20blocks&nba_s_right3_4=M.%20Thornton%205-11%20three%20pointers&nba_s_right3_count=4&nba_s_url3=http://sports.espn.go.com/nba/boxscore?gameId=400278618&nba_s_count=3&nba_s_loaded=true


Il parametro nell'URL è la "/nba/".

Questo programma prende la stringa di output, e la splitta:







Debug delle coppie nome/valore:

nba_s_delay=120
nba_s_stamp=0306072047

nba_s_left1=Boston 109 Philadelphia 101 (FINAL)
nba_s_right1_1=P. Pierce 18pts, 7ast, 11reb
nba_s_right1_2=J. Holiday 18pts, 10ast, 5reb
nba_s_right1_count=2
nba_s_url1=http://sports.espn.go.com/nba/boxscore?gameId=400278616

nba_s_left2=LA Lakers 105 Oklahoma City 122 (FINAL)
nba_s_right2_1=K. Bryant 30pts, 2ast, 3reb
nba_s_right2_2=R. Westbrook 37pts, 5ast, 10reb
nba_s_right2_count=2
nba_s_url2=http://sports.espn.go.com/nba/boxscore?gameId=400278617

nba_s_left3=Denver 120 Sacramento 113 (FINAL)
nba_s_right3_1=T. Lawson 24pts, 7ast, 3reb
nba_s_right3_2=I. Thomas 23pts, 8ast, 3reb
nba_s_right3_3=D. Cousins 5 blocks
nba_s_right3_4=M. Thornton 5-11 three pointers
nba_s_right3_count=4

nba_s_url3=http://sports.espn.go.com/nba/boxscore?gameId=400278618
nba_s_count=3
nba_s_loaded=true




Versione PHP originale, che crea un RSS:
\n\n";
echo "\n\n";
echo "\n\n";
echo "\n";

echo "NBA Scores\n";
echo "http://www.nba.com\n";
echo "NBA Scores\n";
echo "en-us\n";
echo "\n";
echo " NBA Scores\n";
echo " http://www.mpiii.com/scores/nba.gif\n";
echo " http://www.nba.com\n";
echo "\n";
echo "info@nba.com\n";

$content = get_content ("http://sports.espn.go.com/nba/bottomline/scores");

$content_array=explode("&", $content);
$scorearray = array();
$i=0;
foreach($content_array as $content) {
	if (strpos($content, "_left")) {
		$equalpos = strpos($content, "=");
		$end = strlen($content);
		$title = substr($content, ($equalpos+1), $end);
		$title = str_replace("^", "", $title);
		$title = str_replace("%20", " ", $title);
		$scorearray[$i]["title"] = $title;

	}
	if (strpos($content, "_url")) {
		$equalpos = strpos($content, "=");
		$end = strlen($content);
		$url = substr($content, ($equalpos+1), $end);
		$url = str_replace("^", "", $url);
		$url = str_replace("%20", " ", $url);
		$scorearray[$i]["url"] = $url;
				$i++;

	}
}
foreach($scorearray as $score) {
	echo "\n";
	echo "".$score["title"]."\n";
	echo "".$score["url"]."\n";
	echo "\n";
}

echo "\n";
echo "\n";
?>


Trovato qui e qui.

Elenco di tutti i parametri/feed di espn.

Sono i dati utilizzati dall'applicazione ESPN Bottomline.

13 December 2012

Change back to Yahoo Classic Mail

Log into your mail account, disable java script in your browser, hit refresh, and then re-enable java script. its working for me so far.


Al 2012-12-13 funziona!

Trovato qui.