在运行时更改sessionState aspnet的提供者连接字符串

时间:2014-05-21 15:48:51

标签: asp.net asp.net-membership membership-provider

为了使用

更改aspnet成员资格中的提供者的连接字符串

自定义提供程序(nauckit)我使用它:

 var connectionStringField = Membership.Provider.GetType().GetField("m_connectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (connectionStringField != null)
            connectionStringField.SetValue(Membership.Provider, connectionString);

        var roleField = Roles.Provider.GetType().GetField("m_connectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (roleField != null)
            roleField.SetValue(Roles.Provider, connectionString);

        var profileField = ProfileManager.Provider.GetType().GetField("m_connectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (profileField != null)
            profileField.SetValue(ProfileManager.Provider, connectionString);

其中connectionstring是我想要的连接字符串。 但我无法更改sessionState。

我的webconfig是这样的:

<membership defaultProvider="PgMembershipProvider">
            <providers>
                <clear />
                <add name="PgMembershipProvider" type="NauckIT.PostgreSQLProvider.PgMembershipProvider" connectionStringName="myConnection1" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" maxInvalidPasswordAttempts="100" passwordFormat="Hashed" />
            </providers>
        </membership>
        <roleManager enabled="true" defaultProvider="PgRoleProvider" cacheRolesInCookie="true" cookieName=".AspNetRoles" cookiePath="/" cookieProtection="All" cookieRequireSSL="false" cookieSlidingExpiration="true" createPersistentCookie="false" cookieTimeout="30" maxCachedResults="25">
            <providers>
                <clear />
                <add name="PgRoleProvider" type="NauckIT.PostgreSQLProvider.PgRoleProvider" connectionStringName="myConnection1" />
            </providers>
        </roleManager>
        <profile enabled="true" defaultProvider="PgProfileProvider">
            <providers>
                <clear />
                <add name="PgProfileProvider" type="NauckIT.PostgreSQLProvider.PgProfileProvider" connectionStringName="myConnection1" />
            </providers>
            <properties>
                <add name="property1" type="long"/>
            </properties>
        </profile>
        <sessionState mode="Custom" customProvider="PgSessionStateStoreProvider">
            <providers>
                <clear />
                <add name="PgSessionStateStoreProvider" type="NauckIT.PostgreSQLProvider.PgSessionStateStoreProvider" enableExpiredSessionAutoDeletion="true" expiredSessionAutoDeletionInterval="60000" enableSessionExpireCallback="false" connectionStringName="myConnection1" />
            </providers>
        </sessionState>

有任何帮助吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

只需在您网站上的任何IHttpModule中调用global.SetStore。

DAMN我很高兴这很有用。

的Global.asax.cs:

    private FieldInfo StorePrivateMemberInfo  = typeof(SessionStateModule).GetField("_store", BindingFlags.Instance | BindingFlags.NonPublic);
    public void SetStore()
    {

        var store = StorePrivateMemberInfo.GetValue(this.Modules["Session"]);
        if (store == null || !(store is Microsoft.Web.SessionState.SqlInMemoryProvider))
        {
            var config = new NameValueCollection();
            var cnn = "Your connection string";
            config.Add("connectionString", cnn);
            config.Add("timeout", "30");
            simp = new Microsoft.Web.SessionState.SqlInMemoryProvider();
            simp.Initialize("SqlInMemoryProvider", config);
            StorePrivateMemberInfo.SetValue(this.Modules["Session"], simp);
        }

        Session["GLOBAL_ASAX_CHECK"] = true;
    }

Evidence