How to Use A Custom Authentication Provider For The SharePoint 2010 BCS Administration Object Model

The SharePoint 2010 Business Connectivity Services (BCS) are providing an Administration Object Model to manage all kind of BCS objects. You can use the an Administration Object Model to programmatically create BDC models, LOB system and instances, Entities and Methods. The Microsoft SharePoint Designer is using the object model itself to let you generate External Content Types (ECT).

The sample code is this blog entry is a WPF client application which will read all BDC model names and display them in a ListView control. The application allows the user to enter credentials other than the current Windows user:

The starting point to access the BCS data is the AdministrationMetadataCatalog class. This class is part of the Microsoft.SharePoint.BusinessData.Administration.Client.dll library and namespace. You also need a reference to the Microsoft.BusinessData.dll library.

In order to create an instance of the AdministrationMetadataCatalog class with custom credentials we have to call the GetCatalog method. We need to pass a custom authentication provider as parameter to GetCatalog. A custom authentication provider is a class which implements the ILobiAuthenticationProvider interface. The interface is quite simple, it just asks you to return the user ID (including the domain name) and the according password.

Here the code of your custom authentication provider:

1 internal class SharePointConnection : ILobiAuthenticationProvider
2 {
3 // . . .
4
5 public SharePointConnection(string userId, string password)
6 {
7 UserId = userId;
8 Password = password;
9 }
10
11 public AuthenticationScheme GetAuthenticationScheme(string server, string serverUrl)
12 {
13 return AuthenticationScheme.RunAs;
14 }
15
16 public string GetCookie(string server, string serverUrl)
17 {
18 return "BCSCustomAuthenticationProvider";
19 }
20
21 public string GetUserId(string server, string serverUrl)
22 {
23 return UserId;
24 }
25
26 public string GetPassword(string server, string serverUrl)
27 {
28 return Password;
29 }
30 }
Here the sample on how to use the provider:
1 // Create instance of custom authentication provider
2 SharePointConnection spc = new SharePointConnection(UserIdText.Text, PasswordText.Text);
3
4 // Create catalog instance using the custom authentication provider
5 AdministrationMetadataCatalog catalog = AdministrationMetadataCatalog.GetCatalog(SiteURLText.Text, spc);
6
7 ModelsListView.ItemsSource = catalog.GetModels("*").Select(m => m.Name);

Important: If you create your own WPF application using the BCS Administration Object Model and reference the Microsoft.SharePoint.BusinessData.Administration.Client.dll library, you need to set the Platform Target to "Any CPU" in the application settings page, otherwise the project will not compile.

Download Source-Code