Domanda

I have a web application and two sql databases. My client wants different languages and I have solved it with resource files one the public parts of the site, no probs. The thing is they have two databases with the same structure and type of data but in different languages, and the users differs aswell. I have overrided "Initialize" from SqlMembershipProvider as:

public class MyqlMembershipProvider : SqlMembershipProvider
{

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            var connectionString = GetConnectionStringFromSelectedLanguage();

            config["connectionStringName"] = connectionString;
            base.Initialize(name, config);            
        }
}

But the Initialize gets invoked only once, I need to set the connectionString everytime I use Membership.yadayada depending on the language selected. I dont want to use Membership.Providers["one_provider"].DeleteUser(a_username) because It is used EVERYWHERE. How should I go about this, is there anything else I can override?

/Mike

È stato utile?

Soluzione

This isn't possible. The membership provider is a static object initialized once and shared between users. Changing any of the values for it changes them for all users of the application. See this answer for a more thorough explanation...

How do I use multiple databases with sqlmembershipprovider at runtime?

Altri suggerimenti

You could use a DependencyInjection framework like Ninject for the application and build your repositories based on a named dependency.

For example, you could have an Interface which just provides the connection string name:

Interface IConnectionProvider
{
   ConnectionName { get;}
}

class LanguageAProvider : IConnectionProvider
{
    public string ConnectionName { get { return "dbnameLangA"; }  }
}

class LanguageBProvider : IConnectionProvider
{
    public string ConnectionName { get { return "dbnameLangB"; }  }
}

public class MyqlMembershipProvider : SqlMembershipProvider
{
    private readonly string _connectionStringName;

    public MyqlMembershipProvider (IConnectionProvider connection)
    {
        _connectionStringName = connection.ConnectionName;
    }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            var connectionString = GetConnectionStringFromSelectedLanguage();

            config[_connectionStringName] = _connectionStringName;//I'm not sure how you need to use the connection name here.
            base.Initialize(name, config);            
        }
}

I haven't tested any of this, but it could be an idea. You could then have Named Bindings to select the required dependency at run time.

I need to set the connectionString everytime I use Membership.yadayada depending on the language selected

This isn't feasible: consider what happens when two requests from users with different languages are processed concurrently. If you changed the connection string for user A, it would affect user B.

What you could do is write a custom Membership provider which aggregates two (or more) SqlMembershipProviders, and redirects based on the current user's language.

public class MyMembershipProvider : MembershipProvider
{
     private Dictionary<string, MembershipProvider> _languageMembeshipProviders;

     public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        ... create an SqlMembershipProvider for each language and add to
        ... the languageMembeshipProviders dictionary
    }

    public override bool ValidateUser(...)
    {
        ... Get language from HttpContext.Current somehow
        ... select a provider from the dictionary
        ... call the provider
    }
}

However I find it difficult to understand how this could work in practice. Presumably there's something in the request (querystring, cookie, accept-languages header) to indicate the user's language. If so, a user could potentially be authenticated using one language, then switch to access the other database by changing the language in his request.

I'd have thought it would be simpler and cleaner to have separate applications for each language. If a user arrives on site A indicating he wants language B, then he should be redirected to the application B.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top