Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

Using multiple membership providers within ASP.Net MVC 3 application

I am adding a new area to an existing mvc 3 razor app as an admin area for managing some settings and content. The rest of the site is currently secured by a custom provider that authenticates our clients. Now I need to authenticate some employees to access the admin area, The client user data is in a totally different database and format than what I need for our employees.

Is this possible without a lot of headaches?

question from:https://stackoverflow.com/questions/5342158/using-multiple-membership-providers-within-asp-net-mvc-3-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is actually very little headache involved.

Simply add all the MembershipProviders you want in web.config:

<membership defaultProvider="MembershipProvider1">
  <providers>
    <clear/>
    <add name="MembershipProvider1" type="MvcApplication.MembershipProvider1" />
    <add name="MembershipProvider2" type="MvcApplication.MembershipProvider2" />
    <add name="AspNetSqlMembershipProvider"
      type="System.Web.Security.SqlMembershipProvider"      
      connectionStringName="ApplicationServices" />
  </providers>
</membership>

As you can see I have added two providers. I have also specified a default provider.

I can then switch between the two like this:

var membershipProvider1 = Membership.Providers["MembershipProvider1"];
membershipProvider1.ValidateUser("Mike", "banana");

var membershipProvider2 = Membership.Providers["MembershipProvider2"];
membershipProvider2.ValidateUser("Mike", "orange");

You probably want to wrap that into an application service or something, but you get the idea. Not so much headache, right?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...