SSL Certificate Error on .NET

If you see the following message when trying to call a secure web service from .NET applications, it normally is associated with the SSL certificate being expired or not valid for the URL you ‘re calling:

[System.Net.WebException]: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. Inner error [System.Security.Authentication.AuthenticationException]: The remote certificate is invalid according to the validation procedure.

Make sure you really investigate if the certificate is valid.

In a recent case we had to deal with the reason the certificate was throwing this error was the root certificate authority has been changed when the third party service renewed their SSL certificate. The new certificate authority Go Daddy was not recognized by the web server (old Windows 2003 server) and then the server considered the certificate invalid.

It took us awhile to figure it out, but after registering the certificate authority on the server it worked fine.

 

See you,

Amadeu.

Unrecognized attribute ‘targetFramework’.

Some times we receive the following error message when trying to run a .NET 4.0 or 4.5 application:

 

Unrecognized attribute ‘targetFramework’.

 
Error message
 

The cause is normally the application pool configure to run .NET 2.0 application.
 
In order to solve it, go to the application pool for your site and change it to run on .NET 4.0.
 
See you,
Amadeu.

How to Check if .NET 4.5 Is Installed on a Server

In order to check if your server has .NET Framewrok 4.5 installed you can go to the Registry and look for the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client

Check the Version value. If it starts with “4.0” you are running on the 4.0 runtime, if it starts with “4.5” you are running on the 4.5 runtime.

See you,
Amadeu.

HTTP Handlers and SharePoint 2010

Trying to debug an error on a HTTP handler, we got the following error message:

Parser Error Message: Could not create type ‘MyNameSpace.MyClass’.

The HTTP handler directive on the ASHX file looked like this:

<%@ WebHandler Class="MyNameSpace.MyClass" %>

I found the solution for this issue at http://sharepoint.stackexchange.com/questions/19928/sharepoint-2010-and-ashx-handler.

The explanation:

Since we are using SharePoint 2010, the HTTP handler was deployed to the LAYOUTS folder. The trick here is that SharePoint needs to load all its referenced assemblies from the GAC and in this case the assembly was in the GAC but we were not referencing the FQDN of the assembly.

The ASHX file should look like this:

<%@ WebHandler Class="MyNameSpace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c8e2c3ef53023ee" %>

After updating the ASHX file the error was solved.

See you,

Amadeu.

Powershell Script to Check Multiple Files In

Versioning is a very useful feature on document libraries but sometimes when you bulk load a bunch a files you might have a hard time checking it in. In my case I couldn’t disable versioning to upload the files, so I tweaked a script I had to check the files in. If you are in the same situation you may find it a time saver script.

Follow the Powershell script:

$web = Get-SPWeb -Identity “http://%5BSERVER NAME]/[SPWEB]”

$folderURL = “[DOCUMENT LIBRARY]/[FOLDER]”

function ProcessFolder {
param($folderUrl)
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files) {
write-output $file.url
write-output $file.CheckOutStatus
if ($file.CheckOutStatus -ne “None”) {
$file.CheckIn(“”, [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
write-output “Checked file in”
}
}

foreach ($subfolder in $folder.SubFolders)
{
write-output $subfolder.Url
ProcessFolder($subfolder.Url)
}
}

ProcessFolder($folderURL)
 

See you,

Amadeu.

Powershell Script to Download Documents from Document Library

Follow a simple Powershell script to download all documents from a document library. It can be neat when you have large document libraries and don’t want to use Explorer View to transfer files.

 

$destination = “C:\\temp\\”
$web = Get-SPWeb -Identity “http://%5BSERVER NAME]/[SP WEB]”

$folderURL = “[DOCUMENT LIBRARY]/”

function ProcessFolder {
param($folderUrl)
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files) {
#Ensure destination directory
$destinationfolder = $destination + “/” + $folder.Url
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item $destinationfolder -type directory
}
#Download file
$binary = $file.OpenBinary()
$stream = New-Object System.IO.FileStream($destinationfolder + “/” + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}

foreach ($subfolder in $folder.SubFolders)
{
write-output $subfolder.Url
ProcessFolder($subfolder.Url)
}
}

ProcessFolder($folderURL)

 

See you,
Amadeu.

Code to download files from a SharePoint 2010 site using Claims Based Authentication

Trying to download some images from a SharePoint 2010 Image Library using code I was getting HPPT Status 401 – Unauthorized.

First thing I check was the user name and password in a browser window. It worked just fine.

This code used to work for SharePoint 2007 but then I recalled the site uses Claims Based Authentication and it needs a little change in order to authenticate a user on 2010.

Checking the definitions for Windows Authentication requests on SharePoint 2010 I saw we need to specify the X-FORMS_BASED_AUTH_ACCEPTED and the user agent HTTP headers .

Follow a code snippet:

string url = @"File URL";
string userName = "account";
string password = "password";
string domain = "domain";


NetworkCredential credential = new NetworkCredential(userName, password, domain);
WebClient client = new WebClient();
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
client.Credentials = credential;
byte[] data = client.DownloadData(url);


After this code change it worked just fine.

See you,

Amadeu.