Saturday, May 24, 2008

Retrieve the Membership provider name of a Windows SharePoint Services 3.0 or MOSS Extranet Web Application

Introduction :
This post show how to retrieve the Membership provider name of a MOSS or Windows SharePoint Services 3.0 Web Application by server side C# programming. For example , knowing the Membership provider name you can retrieve in your Web Application web.config the connection string name of the data base used for Form Based Authentication Mode.Then you can make requests to this database.
The following code sample enumerate all Web Application zones. It retrieves the Form Based Authentication zone.Then it retrieves the name of the Membership provider.
code sample:
        Using Microsoft.SharePoint;
        Using Microsoft.Sharepoint.Administration;
        .
        .
        .

        string memberShipProviderName = string.Empty;

        SPWeb myWeb = SPContext.Current.Web;

 

        System.Collections.Generic.Dictionary<SPUrlZone, SPIisSettings> myDictionnary = new System.Collections.Generic.Dictionary<SPUrlZone, SPIisSettings>();

 

        myDictionnary = myWeb.Site.WebApplication.IisSettings;

 

        foreach (Microsoft.SharePoint.Administration.SPUrlZone aZone in Enum.GetValues(typeof(Microsoft.SharePoint.Administration.SPUrlZone)))

        {

 

            if (myDictionnary.ContainsKey(aZone))

            {

                if (myDictionnary[aZone].AuthenticationMode == System.Web.Configuration.AuthenticationMode.Forms)

                {

                    memberShipProviderName = myDictionnary[aZone].MembershipProvider.ToString();

                }

            }

        }

1 comment:

Anonymous said...

Very Useful.
Thanks