Quick Tip: Getting MembershipProvider for SPWebApplication
When constructing code leveraging the ASP.NET 2.0 membership model that SharePoint employs, you will in due course want to hydrate a System.Web.Security.MembershipProvider object. If you have written custom membership providers, this abstract class should already be recognizable with since its members are implemented in derived provider subclasses.
Ordinarily, you hydrate a strongly typed reference to a MembershipProvider object by providing an uncomplicated index operation on the Membership.Providers collection, austerely supplying the name of the provider:
-
System.Web.Security.MembershipProvider memProvider = System.Web.Security.Membership.Providers["providername"];
Not very fancy :)
An alternative is viable to this by providing two strongly typed parameters, namely a SPWebApplication and SPUrlZone, to return the strongly typed MembershipProvider. To do this, we can use a comparable approach as above, executing an index on the Membership.Providers collection. However we can automate the retrieval of the provider name using the SPWebApplication.GetIisSettingsWithFallback method consuming the SPUrlZone parameter. Subsequently, the MembershipProvider can be queried:
-
public static MembershipProvider BuildMembershipProvider(SPWebApplication app, SPUrlZone zone)
-
{
-
return Membership.Providers[app.GetIisSettingsWithFallback(zone).MembershipProvider];
-
}
Related posts:
- Return IIS Settings For A SP Zone
- Using Generics To House SharePoint Objects
- Enumerating All SPWebs In SPFarm.Local Into Strongly Typed Collection
- Using DescriptionAttribute And Reflection For Building Typed Collections Off Enumerations
- Returing Virtual Directory Path From Web Application
2 Comments »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Research Methodology
Personal/Off-Topic
Article Or Research Filed Under 

Awesome timing, Adam – I was planning to refactor some code this morning around my own ugly implementation! GetIisSettingsWithFallback will provide a much cleaner way to dynamically find the URL of a specific zone based on the configured authentication mode and membership provider, and I would have completely missed this method without your post.
Comment by Dan Kahler — March 3, 2009 @ 9:30 am
[...] Quick Tip: Getting MembershipProvider for SPWebApplication [...]
Pingback by Links (3/4/2009 « Steve Pietrek - Everything SharePoint) — March 4, 2009 @ 7:30 pm