views:

92

answers:

1

I would like to use .Net's SqlMembershipProvider and SqlRoleProvider for user management in my application. My issue is that when the application starts, it does not know any db connection information. For security purposes, it needs to get this information from a WCF service that is running on the datbase server. Therefore I need to build my membership/role providers after-the-fact.

I think I've been able to work out creating and adding the membership provider:

// register membership provider
var membership = new SqlMembershipProvider();
var providerValues = new NameValueCollection();
providerValues.Add("name", "sqlMembershipProvider");
providerValues.Add("applicationName", "/");
providerValues.Add("connectionStringName", "connectionStrDynamAddedToConfig");
providerValues.Add("maxInvalidPasswordAttempts", "10");
membership.Initialize("sqlMembershipProvider", providerValues);

I have, so far, been unable to work out something similar to create the RoleProvider. I can create the provider, but cannot add it to the Roles Manager. Do I need to create a custom provider that can take a connectionString after it is already initialized?

A: 

I ran across this page, which recommends "downloading the ProviderToolkitSamples and modifying the SQLConnectionHelper Class. Specifically, the GetConnectionString function which looks something like this"

internal static string GetConnectionString(string specifiedConnectionString, bool lookupConnectionString, bool appLevel)
{
        if (specifiedConnectionString == null || specifiedConnectionString.Length < 1)
            return null;

        string connectionString = null;

        /////////////////////////////////////////
        // Step 1: Check <connectionStrings> config section for this connection string
        if (lookupConnectionString)
        {
            ConnectionStringSettings connObj = ConfigurationManager.ConnectionStrings[specifiedConnectionString];
            if (connObj != null)
                connectionString = connObj.ConnectionString;

            if (connectionString == null)
                return null;
        }
        else
        {
            connectionString = specifiedConnectionString;
        }

        return connectionString;
    }
}

Text lifted from williablog.net, since as that page says, "links have a way of breaking over time"

Greg