Monday, June 22, 2009

SharePoint Designer 2007 is NOW a FREE Download

A great tool and very happy now it is free, heaps of THANKS Microsoft.

Check out this video :)

Sunday, June 7, 2009

Retrieve Username and Password from SharePoint Single Sign-on (SSO) Provider Application

I’ve created a helper component SharePointMalaya.SSOHelper.SSOConfiguration that helped to provide the specific methods to retrieve username and password from the given SharePoint SSO provider application name.

The SharePointMalaya.SSOHelper.SSOConfiguration source code details are shown below:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;

using Microsoft.SharePoint;
using MSSO = Microsoft.SharePoint.Portal.SingleSignon;

namespace SharePointMalaya.SSOHelper
{
    /// <summary>
    ///
    /// </summary>
    public class SSOConfiguration
    {
        #region Member Variables

        private string _username;
        private string _password;
        private bool _isexistssoprovider;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SSOConfiguration"/> class.
        /// </summary>
        /// <param name="ssoApplicationName">Name of the sso application.</param>
        public SSOConfiguration(string ssoApplicationName)
        {
            try
            {
                ConnectToSSO(ssoApplicationName);
            }
            catch (Exception ex)
            {
                throw ex;
            }           
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the username.
        /// </summary>
        /// <value>The username.</value>
        public string Username
        {
            get { return _username; }
        }

        /// <summary>
        /// Gets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get { return _password; }
        }

        /// <summary>
        /// Gets a value indicating whether SSO provider is exist.
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance is exist SSO provider; otherwise, <c>false</c>.
        /// </value>
        public bool IsExistSSOProvider
        {
            get { return _isexistssoprovider; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Connects to SSO.
        /// </summary>
        /// <param name="ssoApplicationName">Name of the sso application.</param>
        private void ConnectToSSO(string ssoApplicationName)
        {
            try
            {
                MSSO.ISsoProvider provider = MSSO.SsoProviderFactory.GetSsoProvider();

                if (provider != null)
                {
                    MSSO.SsoCredentials creds = provider.GetCredentials(ssoApplicationName);

                    IntPtr pUserName = IntPtr.Zero;
                    IntPtr pPassword = IntPtr.Zero;

                    try
                    {
                        // Get the non-secure string version of the credentials 
                        pUserName = Marshal.SecureStringToBSTR(creds.UserName);
                        _username = Marshal.PtrToStringBSTR(pUserName);

                        pPassword = Marshal.SecureStringToBSTR(creds.Evidence[1]);
                        _password = Marshal.PtrToStringBSTR(pPassword);

                        _isexistssoprovider = true;
                    }
                    finally
                    {
                        //Zero out and free the BSTR pointers
                        if (IntPtr.Zero != pUserName)
                        {
                            Marshal.ZeroFreeBSTR(pUserName);
                        }

                        if (IntPtr.Zero != pPassword)
                        {
                            Marshal.ZeroFreeBSTR(pPassword);
                        }
                    }
                }
            }
            catch (MSSO.SingleSignonException ssoEx)
            {
                throw ssoEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}

The following tables list the members exposed by the SharePointMalaya.SSOHelper.SSOConfiguration type.

Public method SSOConfiguration - Initializes a new instance of the SSOConfiguration class, connects to SSO provider and get the username and password.

Public method Username - Gets the username from SSO provider.

Public method Password - Gets the password from SSO provider.

Public method IsExistSSOProvider - Gets a value indicating whether SSO provider is exist.

Example

The following is code sample on how to connect to the SSO provider and retrieve stored SSO username and password:

if (!string.IsNullOrEmpty(txtSSOApplicationName.Text))
{
    // Instantiate SSOConfiguration and connect to SharePoint SSO provider by passing SSO Application Name
    SSOConfiguration objSSOConfiguration = new SSOConfiguration(txtSSOApplicationName.Text);

    // Check if SSO provider is found
    if (objSSOConfiguration.IsExistSSOProvider)
    {
        // Assign stored SSO Username and Password to controls
        lblSSOUsername.Text = objSSOConfiguration.Username;
        lblSSOPassword.Text = objSSOConfiguration.Password;
    }
}

The following are screenshot showing a user logon to SharePoint site and retrieve his/her username and password from “SSO_Provider1” SSO Provider application name.

SSO1

Get source code here: