Wednesday, May 9, 2012

Retrieving Credentials from Secure Store Service

MOSS 2007 makes use of Single Sign On Service which has been replaced by Secure Store Service in SharePoint 2010. Secure Store Service can be used authenticate External Applications.
To see how to configure SSS please visit Configure the Secure Store Service SharePoint 2010


We will create a custom web part to retrieve current user credentials.
Open Visual Studio and create new Empty SharePoint Project.
Add new Visual webpart.
Add reference to following dll
  • Microsoft.BusinessData (C:\Windows\assembly\GAC_MSIL\Microsoft.Office.SecureStoreService\14.0.0.0__71e9bce111e9429c\Microsoft.Office.SecureStoreService.dll )
  • Microsoft.Office.SecureStoreService.Server
    (C:\Windows\assembly\GAC_MSIL\Microsoft.BusinessData\14.0.0.0__71e9bce111e9429c\Microsoft.BusinessData.dll)
  • Microsoft.SharePoint
Add following method in visual webpart's usercontrol code behind

 private static string GetStringFromSecureString(SecureString secStr)
        {
            if (secStr == null)
            {
                return null;
            }

            IntPtr pPlainText = IntPtr.Zero;
            try
            {
                pPlainText = Marshal.SecureStringToBSTR(secStr);
                return Marshal.PtrToStringBSTR(pPlainText);
            }
            finally
            {
                if (pPlainText != IntPtr.Zero)
                {
                    Marshal.FreeBSTR(pPlainText);
                }
            }
        }

Now in user control design add two labels "lblUsername" and "lblPassword". Then in code behind file add following code in page load.

string _TargetApplicationID = "MyCustomApp";

            ISecureStoreProvider _ISecureStoreProvider = SecureStoreProviderFactory.Create();
            if (_ISecureStoreProvider == null)
            {
                throw new InvalidOperationException("Unable to get an ISecureStoreProvider");
            }

            ISecureStoreServiceContext _ISecureStoreServiceContext = _ISecureStoreProvider as ISecureStoreServiceContext;
            _ISecureStoreServiceContext.Context = SPServiceContext.GetContext(SPContext.Current.Site);
           
            try
            {
                SecureStoreCredentialCollection _SecureStoreCredentialCollection = _ISecureStoreProvider.GetCredentials(_TargetApplicationID);
                if (_SecureStoreCredentialCollection != null)
                {
                    foreach (SecureStoreCredential _SecureStoreCredential in _SecureStoreCredentialCollection)
                    {
                        if (_SecureStoreCredential == null)
                            continue;

                        switch (_SecureStoreCredential.CredentialType)
                        {
                            case SecureStoreCredentialType.UserName:
                                 lblUsername.Text = GetStringFromSecureString(_SecureStoreCredential.Credential);
                                break;

                            case SecureStoreCredentialType.Password:
                                  lblPassword.Text = GetStringFromSecureString(_SecureStoreCredential.Credential);
                                break;
                        }
                    }
                }
           }
            catch (SecureStoreException e)
            {
                throw;
            }



2 comments:

  1. Can you please tell me what will be the appId.
    Or from where I could get an appId.

    ReplyDelete
    Replies
    1. its application id for details please visit http://www.anmolrehan-sharepointconsultant.com/2012/05/configure-secure-store-service.html

      Delete

Popular Posts