Change ApplicationName application providers at run time. How?

I have a slightly unique situation here. I am making a web application that will be able to login with the credentials of different web applications. For example, you can login / register on my site, or you can login / register in your YouTube account. I do not use OpenID, because in this case I need to have access to YouTube data.

I am using ASP.NET MVC 3 EF4 with custom members, roles, profile providers.

The problem is that usernames cannot be unique, because someone with a YouTube username may have the same username as on my site. Thus, I walked around by specifying the user type in my user table. This is a fairly complex key (user ID and user type).

I have a custom authorize attribute that checks the role the user is in, but now I need to implement a custom IPrincipal because I need to pass the user type. Only problem is where do I store it? session?

Initially, I thought that this was what the application table was for, and I had short-term success, but I read that there were problems with the threads, and I was getting session errors everywhere, it wasn’t so cool :(

I am wondering what is the best way to do this because I cannot use the overridden methods of the providers, because I need to add the UserType parameter to some of the methods, but then this violates the functionality of the provider.

EDIT: I basically should be able to change the application name at runtime programmatically. I tried to do this, the only problem was that when I stopped my development server but left my browser open and then started my dev server again, it did not save the application name.

EDIT: I changed my application to use OAuth, I did not find a good solution.

+7
source share
3 answers

I basically need the ability to change the application name at runtime programmatically. I tried doing this, the only problem was when I stopped my development server but left my browser open and then started my dev server again there would be no application name.

If you need to change the name of ApplicationName, this means that you need to select a provider at runtime. The only way to do this is NOT to use the Singleton "Membership" as it uses the provider defined in web.config.

Instead, every time you need your provider, use:

MembershipProvider userProvider = Membership.Providers[UserProviderName]; 

Just set UserProviderName the way you want. I would go with a custom global authorization filter or preAction that would detect the provider from some cookie or other session variable and put the provider in the HttpContextBase.Items collection, which lives for only one and only one request.

+5
source

The best answer to this problem was answered by stackoverflow here: Membership provider with a different application name in scope

Here is the code they used:

 Membership.Providers["MyOtherProvider"].ValidateUser(username, pwd); 
+1
source

Ryan

Hmmm ... can you solve this problem by adding a listing (local or YouTube) to the username field ... Username example: "LOCAL / corlettk", "YOUTUBE / corlettk" ???

Well, you will need a custom Authenticator to break the complex string and skip the input request for the corresponding basic Authenticator ... but once that is done, (I think), you are all set to resolve EASY with a much bigger (from your point of view) authorization .

I suppose you're a smart guy ... have you considered and fired this approach? [/ p>

Greetings. Whale.

PS: Yes, I'm a hacker ... but I have a bad habit of hacking things that WORK ... so they gave up trying to educate me.

-one
source

All Articles