简体   繁体   English

在不同的Db中创建MembershipUser,然后在web.config中

[英]create MembershipUser in different Db then is in web.config

MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text);

In web.config i have connection string but i would like to use MemBershipUser on different connection string. 在web.config中,我有连接字符串,但我想在其他连接字符串上使用MemBershipUser。 Is that possible, and if yes how? 有可能吗?如果可以,怎么办?

regards 问候

Just add an extra connectionstring and use that in your membership configuration. 只需添加一个额外的连接字符串,然后在您的成员资格配置中使用它即可。 Look at this example: http://msdn.microsoft.com/en-us/library/6e9y4s5t.aspx As you can see, the membership section references its connection string by name. 看下面的示例: http : //msdn.microsoft.com/zh-cn/library/6e9y4s5t.aspx如您所见, membership部分按名称引用其连接字符串。

found the solution if someone else has similar problem: 如果其他人有类似问题,找到解决方案:

A simpler, albeit somewhat eyebrow-raising solution is just modifying the connection string in the providers early enough in the request's lifecycle: 一个比较简单的解决方案,尽管有点让人大跌眼镜,但它只是在请求的生命周期中尽早修改了提供程序中的连接字符串:

      private void SetProviderConnectionString(string connectionString)
    {
        // Set private property of Membership, Role and Profile providers.
        var connectionStringField = Membership.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
        if (connectionStringField != null)
            connectionStringField.SetValue(Membership.Provider, connectionString);

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM