How do I reach the middle tier using the memship class? - asp.net

I have a 3-tier ASP.NET 2.0 app. I want to use the Membership.ValidateUser method of the membership class using the credentialls added with the login control. As stupid as it seems, I can't figure out how to have the ValidateUser control call anything but the db specified in the web.config. What I need is it to call down to the middle tier which will authenticate against the db. I can't have the presentation layer authenticate against the db directly.

You just need to create a custom membership provider, inherit from MembershipProvider then wire it up in the web.config. The provider could go in your App_Code folder then call your middle tier
<membership defaultProvider="CustomProvider">
<providers>
<add
name="CustomProvider"
type="YourNameSpace.YourCustomProvider"
connectionStringName="ConnectionString" />
</providers>
</membership>

Related

MVC4 Simple Membership Provider setup

I would like to use mvc4 Simple membership provider. So I set up a new MVC4 internet application. Click Debug and I see that db and tables were created for me.
Is this all I need to do?.
Some articles:
http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx
http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/
tell me to add
<membership defaultProvider >...
section. is this necessary?
Also:
I can't get ASP.Net configuration tool to work. It says "An error was encountered. Please return to the previous page and try again. "
??
If you created a new MVC 4 web application using the Internet template it should have wired up SimpleMembership for a basic log-on/log-off functionality. It should have already updated your web.config with the proper settings. It should have setup the membership and roles and they should looks something like this.
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear/>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear/>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
SimpleMembership does not use the ASP.NET Configuration Tool used with the traditional ASP.NET membership. You have to create your own web pages for managing roles and users.
The Internet template just creates the basic functionality for authentication and authorization in your application. But SimpleMembership is very extensible. For example it is fairly easy to customize what type of information you want to capture in the user profile or setup email confirmation of new users.

simple membership provider in mvc

How to make simple Membership provider from empty web application template in ASP.NET MVC4
I searched a lot on google, bing and many others, but I din't get positive responce about membership provider
can some one tell me basic of membership provider?
please
I followed these steps:
So before starting I am assuming you have setup your database models including a users model which we will use for simple membership. Go ahead and add a "username" column and an "id" column (if you don't already have one) in the model and create the database. Remember already having a database is necessary if you want to use simple membership with your existing user's table. Simple membership will add it's table to your existing database.
1.Install Webmatrix.webdata and webmatrix.data from Nuget Packet manager.
2.In your web.config enable simple membership by
<add key="enableSimpleMembership" value="true" />
in appsettings
3.Next step is to define profile, role and membership providers in system.web by
<profile defaultProvider="SimpleProfileProvider">
<providers>
<add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="YOUR_CONNECTION_STRING" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
<roleManager enabled="true">
<providers>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
4.Next step is to connect your user table with simple membership. Now the Internet Application Template which is being provided by default uses a filter but we're going to skip it. We're going to directly initialize simple membership in global.asax by
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("YOUR_DB_CONTEXT", "USER_TABLE", "ID_COLUMN", "USERNAME_COLUMN", true);
}
And you are done with the setup.
Now to actually create users, there are two options, Create User and Account and Create Account only. How the create user and account works is that it will you will provide the user's information and it will create a user in your user table and then create a membership account. I personally use the latter, which means I create the users separately, and then create a membership account for that user using
WebSecurity.CreateAccount("Username","Password");
Just create an Internet template, and copy the code out of it into your empty project.. although, honestly at that point you've essentially got the Internet template anyways, other than the default layout.
There's a lot of code that goes into supporting the Membership system, so study the Internet template and it will tell you everything you need to know.
When you create the new project, select the Internet Template.
When you register your first user it will automatically create the table structure in your db
So you want to use simple membership in asp.net MVC4 .
Follow the steps mentioned in this tutorial : Simple Membership
This will provide the all the basic information of how to setup simple membership in asp.net mvc4.

Can I use multiple MembershipProviders at one time?

I've got multiple membership providers in my web.config and in my login control,
I am going to use the provider based on a drop down list with the name of the provider.
Web.config:
<system.web>
<membership>
<providers>
<remove clear />
<add name="MyOwnProvider1" .... />
<add name="MyOwnProvider2" .... />
</providers>
</membership>
</system.web>
In Login.ascx.cs:
I am selecting the provider based on a drop down list like so:
MembershipProvider provider = Membership.Providers[dropDownList.SelectedItem.Text];
Problem is whenever I hit this line, it always tries to connect to MyOwnProvider1 when in fact MyOwnProvider2 was selected!
Any ideas?
The cause of the problem you are having is that when the app is spun up, either the provider flagged as defaultProvider in the membership element OR the first provider encountered, starting with your web.config and moving upstream to the root web.config in the .net framework/config directory, is initialized, making it the membership provider.
Couple this behavior with the fact that all of the baked in plumbing and controls are expecting to work with a single provider and you are uscwap.
In order to make something like this work, you are going to have to implement a single custom membership provider that acts as a facade or aggregator for your multiple authentication sources and add that as the single provider in web.config.
Cheers
Is it possible to dynamically select a provider that way? I've always assumed not (though I've never tried it), in this instance I'd guess that when it loads Membership.Providers it stops at the first one it comes to, MyOwnProvider1 in your case.

Custom Role Provider with ActiveDirectory Authentication

I'm creating a custom Role provider based on the ASP.NET Role provider. I have 3 tables. One for Users, one for Roles, one for UsersInRoles.The Users table has no password column because the users are authenticated with ActiveDirectory. That's my approach so far. I can't get the cusstom Role Provider to work, anyone has the same situation like me. How do you make a custom Role provider works with AD?
What I did: create a class which inherits from System.Web.Security.RoleProvider, and choose "Implement abstract class" from the context menu when clicking on : Roleprovider. I only implemented the method GetRolesForUser (the other methods throw NotImplementedException).
At a certain point I thought I also needed to implement the MembershipProvider, but a simple addition to web.config fixed it (since the assembly is not in the GAC, in the type-attribute, you only need to mention the namespace+type-name; not the assembly name and other parameters):
<configuration>
<system.web>
<roleManager enabled="true" defaultProvider="MyRoleProvider">
<providers>
<clear />
<add name="MyRoleProvider" type="Namespace.To.MyRoleProvider" />
</providers>
</roleManager>
</system.web>
</configuration>
There is no need to implement the ValideUser method on a MembershipProvider.
You should be able to write the role provider in a manner to where you override the ValidateUser() method and force it to perform the AD lookup there. After that, most of the built in stuff should take over.

How do I deploy an ASP.net custom MembershipProvider?

I've written a custom MembershipProvider that uses a custom database schema for storing the members, but I am having trouble figuring out how to deploy the provider. My target server is running IIS7, and I am able to navigate to a dialog for a adding a .NET User Provider, but instead of allowing me to select the assembly containing the provider & then the class, it provides a drop-down with a couple of MS written providers.
Do I need to drop my assembly in a specific location so that my MembershipProvider class is discovered by IIS? If so, what where does the .dll need to go? Otherwise, how do tell ASP.Net to use my MembershipProvider? Every example I've seen simply references the fully qualified class name, but makes no mention of how the file needs to be deployed.
If you look in the web.config file for your application, you should have a section called system.web. Within that there is a membership element with a list of providers. You should be able to add your provider and set a default provider there. Once your membership provider is registered in this way, you should be able to select it as a default for that application from IIS as well.
<system.web>
...
<membership defaultProvider="MyMembershipProvider"
userIsOnlineTimeWindow="15">
<providers>
<add name="MyMembershipProvider"
type="Common.Auth.MyMembershipProvider, Common"
connectionStringName="MyAuthDBConnectionString"
enablePasswordRetrieval="true"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
writeExceptionsToEventLog="false" />
</providers>
</membership>
...
</system.web>
The providers element allows you to register multiple providers to choose from. Another feature is that you can clear out membership providers registered in other configuration files on the machine. This can make configuring your application less error prone later on. To do so, add the <clear/> element before the first membership provider (the <add/> element) in the list.
<system.web>
...
<membership defaultProvider="MembershipProvider1">
<providers>
<clear />
<add name="MembershipProvider1" ... />
<add name="MembershipProvider2" ... />
</providers>
</membership>
...
</system.web>
If you want to register the same provider with multiple web applications just using IIS Manager, you will need to put the assembly in the GAC and add the provider to one of the machine config files instead. This is usually more work for little benefit when deploying a single application.

Resources