Thursday, January 31, 2008

Writing a simple SSOProvider for BDC in Sharepoint 2007

Here's a simple code which i wrote to enable BDC call webservices which support basic authentication.

I started with msdn sample for a simple pluggable SSOProvider and hardcoded the user according to my own needs.
I was getting an exception when Sharepoint tried to call this SSOProvider as it expects that GetRestrictedCredentials method of ISSOProvider is also implemented. I just returned the output of GetCredentials from this method and was able to call the service successfully.
Here's the code for reference ....


public class BasicAuthentication : ISsoProvider
{
#region ISsoProvider Members
public SsoCredentials GetCredentials(string AppID)
{
try
{
SsoCredentials ssoCreds = null;
switch (AppID)
{
case MyAppName:
{
//this is our application so return the basic credentials
ssoCreds = CreateSsoCredentials(AppID);
break;
}
default:
{
//not our application so let's not even let it have access to our SSO
throw new SingleSignonException(SSOReturnCodes.SSO_E_ACCESSDENIED);
}
}
//return the created SSO Credentials
return ssoCreds;
}
catch (SingleSignonException singleSignonEx)
{
EventLog.WriteEntry("GetCredentials has thrown SSO Exception");
throw;
}
catch (Exception exception)
{
EventLog.WriteEntry("SimpleSSOProvider", exception.ToString());
throw new SingleSignonException(SSOReturnCodes.SSO_E_EXCEPTION, exception);
}
}
private SsoCredentials CreateSsoCredentials(string AppID)
{
SsoCredentials ssoCreds = new SsoCredentials();
ssoCreds.Evidence = new SecureString[2];
ssoCreds.Evidence[0] = MakeSecureString("D034677M1");
ssoCreds.Evidence[1] = MakeSecureString("Welcome1");
// Put the User Name and Password values into the credential object
ssoCreds.UserName = ssoCreds.Evidence[0];
ssoCreds.Password = ssoCreds.Evidence[1];
return ssoCreds;
}
private SecureString MakeSecureString(string sourceString)
{
if (sourceString == null)
{
//we don't have a good source string so return null
return null;
}
SecureString secureString = new SecureString();
foreach (char ch in sourceString)
{
//append each character in the source string to the end of our secure string
secureString.AppendChar(ch);
}
return secureString;
}
public SsoCredentials GetRestrictedCredentials(string AppID)
{
return this.GetCredentials(AppID);
}
public SsoProviderInfo GetSsoProviderInfo()
{
//This method is required for all SSO Providers.
SsoProviderInfo ssoProvInfo = new SsoProviderInfo();
//setup the information about our sso provider
ssoProvInfo.AssemblyName = Assembly.GetExecutingAssembly().FullName;
ssoProvInfo.Vendor = MyAppName;
ssoProvInfo.Version = "1.0.0.0";
return ssoProvInfo;
}
private const string MyAppName = "MyApp";

////Continue to implement other methods of the interface
////You can safely throw NotSupportedException from these methods...
}


~Abhishek

No comments: