Tuesday, December 25, 2007

WCF with Transport security and self-signed certificate

Say you want to create a secure WCF channel and not having the client verifying the server's certificate (maybe because you are testing or the server's certificate is self-signed and you don't want to install that certificate in the client's trusted certificate's store).

1) Configure the server this way:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyContract">
<endpoint
address=""
binding="basicHttpBinding"
bindingConfiguration="NtlmSecurity"
contract="IMyContract" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="NtlmSecurity">
<security mode="Transport">
<transport clientCredentialType ="Ntlm" />
</security>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>


2) Force not verifying the server's certificate. WARNING: this is intended only for test purposes. In production code, you don't want to avoid checking server's certificate at all.

private IMyContract GetChannel(Uri address)
{
// Create an endpoint address
EndpointAddress endpoint = new EndpointAddress(address);

// Create a binding
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(
delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
return (true);
});

// Create a channel to the service application endpoint
ChannelFactory<imycontract> factory = new ChannelFactory<IMyContract >(binding, endpoint);
return factory.CreateChannel();
}

Friday, December 21, 2007

SharePoint: How to add a web part to a page from a solution package

Assume you already got a solution package (.wsp) of the web part.

First, you have to install it:

SET STSADM="%full path%\STSADM.EXE"
SET URL="http://urlOfMySite"
SET SPACK="myWebPart.wsp"

%STSADM% -o retractsolution -name %SPACK% -immediate -url %URL%
%STSADM% -o execadmsvcjobs

%STSADM% -o deletesolution -name %SPACK% -override
%STSADM% -o execadmsvcjobs

%STSADM% -o addsolution -filename %SPACK%
%STSADM% -o execadmsvcjobs

%STSADM% -o deploysolution -name %SPACK% -immediate -allowgacdeployment -url %URL%
%STSADM% -o execadmsvcjobs

Then, from the site where you want to add the web part to a page:

Site Actions | Site Settings | Modify All Site Settings | Galleries | Web Parts, New
Choose your web part, click "Populate Gallery"

Then,

Site Actions | Site Settings | Modify All Site Settings | Site Collection Administration | Site Collection Features, click "Activate" to activate your web part

Then, you should be able to add your web part to a page on that site:

Navigate to a page
Site Actions | Edit Page, Add a Web Part, choose your web part, Add, Publish

Friday, December 14, 2007

System.Web.Extensions version error

Problem:

when trying to access a SharePoint page (can repro with plain ASP.NET too), this exception got thrown:

An error occurred during the processing of . (0): error CS1705: Assembly 'Microsoft.Whatever' uses 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

Solution:

in affected web.config, replace

"System.Web.Extensions, Version=1.0.61025.0"

with

"System.Web.Extensions, Version=3.5.0.0"

and iisreset.

Wednesday, December 12, 2007

Visual Studio Find & Replace with Tagged Expressions

This is an example on how to use tagged expressions in Visual Studio Find & Replace dialog.

Say you need to replace all of the ocurrences of

MyMethod<MyType>("Identity");

with something like

(MyType)Fields["Identity"];

Then, you have to find

MyMethod\<{:w+}\>\(\"{:w+}\"\);

and replace it by

(\1)Fields["\2"];

Simple.

Notice two things here:
  1. the backslashes before every reg exp metacharacter in the Find statement
  2. the absence of them in the Replace statement