Asp.net MVC 4 EF Code First Membership Providers for Beginners - asp.net-mvc

Though I asked few questions about the how to use membership providers with ASP.NET MVC 4 but hardly got any response. It is taking me time (as a newbie) to get started with membership providers in ASP.NET MVC 4. I have been reading article, trying stuff but that only added to the confusion rather than helping.
What would be the answers to the following queries by a beginner:
I want to try all the membership providers (including the simple membership provider) before deciding which one should I use for which project. What membership providers can I use with ASP.NET MVC 4 and How can I configure each one of it. Any documentation or links?
Though the internet template comes with pre-configured Account controller which sets the membership functionality, but how can I add membership functionality to the basic template. What steps should I follow in order to configure membership using membership providers (I assume there will be different set of steps/configuration/settings for different membership provider).
Running the ASP.NET MVC 4 project created with internet project template (without making any changes to it) create 5 tables in the database namely UserProfile, webpages_Membership, webpages_OAthMembership, webpages_Roles, webpages_UsersInRoles). In AccountModel.cs I see definition for only one, UserProfile. Where are the other tables defined. Do I always need to have all these tables in database in order to use membership. Can I customize them, rename them or choose not to use some of them?
I do not see any membership configuration in web.config for the internet project template yet is using membership. In some of the articles tutorial I saw people configuring it by adding <membership></membership> to web.config. When do I need to add configuration settings to web.config and when do I need not?
I think membership providers are meant to make developers life easy yet for me it is making it more difficult.
I know I really need to study it and that's what I am trying for the last 7, 8 days but unable to figure out.
Any help, links (hopefully not those which I went through many times) are more than welcome.

I agree with you, the guidance around what membership types do what just sucks.
You can use any of the providers with ASP MVC. The default template in Visual Studio that is the Internet MVC app is the only one that uses SimpleMembership.
Good question, I've not found any explicit, decent documentation on that at all. As far as I can tell most of the membership things are setup very similar.
At least SimpleMembership will create the needed DB tables that are required if they do not exist. I think the only one you can really customize is your user table.
I think like the sql tables, the membership tags in the web.config are implied. If you accept the defaults they magically work.
Best link I've found is this
http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/
In that post there are two others that are helpful as well.

For question 3, I tried something and it turned out that:
Yes: You can add new columns to UserProfile (and all other tables, I guess)
No: You cannot rename the table, even if u have modified the codes in AccountModels to match new table names.
In this case, a set of tables with old names will be created again when you launch application. And all codes provided by MVC4(MembserShip, WebSecurity, Users, Roles....) will used the old tables.
And, I have never found any information about how to rename these tables.

Related

Is there a simple way to add a model in ASP.NET Core MVC via the GUI, besides the package manager console?

At work, I use ASP.NET MVC (not .NET Core) with Entity Framework and SQL Server. However, I wanted to practice a bit with ASP.NET Core MVC and some other SQL Server stuff (like experimenting with SSIS) at home, so I set up a small dev environment for me to work in. I created my database and tables, populated it with information, and created the default template you get with ASP.NET Core MVC. No issues so far!
But then, when I went to add a model into my project, I had no option to add an ADO.NET Entity Data Model like I do at work. After googling around a bit, I saw a mention that you could not create these kinds of models in .NET Core applications: https://learn.microsoft.com/en-us/answers/questions/357012/can39t-find-adonet-entity-data-model-missing-visua.html. I did some more googling, and it seems like just about every single thing I'm finding online is that you have to use package manager console, and type it all out.
Is this really the only way? Surely there has to be something better... It was very nice in ASP.NET MVC when I could add the model, and then it would take me through a wizard to get everything set up. I could create a new connection string, test the connection to the database, select which tables/views I wanted to add to the model, and I was all set! It was just as nice being able to go into my model and easily update the model with new tables, columns, or anything else I needed.
I get that code-first solutions are more mainstream these days, but I want to stick with the database first approach. Is there a simple, user-friendly (non package-manager console) approach for me to add models like I used to, but still get the benefits of .NET Core? Any good tutorials out there to get me on the right track (that aren't code first)? I appreciate anyone who can point me in the right direction!

MVC Simple Membership Web Site Administration Tool

I must have missed this somewhere (or slept through the class)... the standard ASP security tool for managing users and roles seems to be absent when using the new simple security version. That is, the original is there and working, just points to traditional ASP security... NOT to the new "simple" system.
In my project I can log in, I can look at the webpages_ tables but can find no method of accessing them in my project... I would be happy to manage roles and get the USERID and maybe ROLEID so I could tie the user to other functions in my project.
I've looked through hundreds of posts and articles... normally I find I am missing some minor config entry or something but this time I am a little more stuck.
Advice is appreciated.
Thanks!
SimpleMembership does not work with the Web Site Administration Tool (WSAT). There is a good overview of SimpleMembership in this blog. Here is a quote from it.
WSAT is built to work with ASP.NET Membership, and is not compatible with Simple Membership. There are two main options there:
Use the WebSecurity and OAuthWebSecurity API to manage the users and
roles.
Create a web admin using the above APIs.
Since SimpleMembership
runs on top of your database, you can update your users as you would
any other data - via EF or even in direct database edits (in
development, of course)
I would also add that you can seed some of this information at application start-up, such as what roles are available, as shown in this article.

Is there any out of the box admin page for SimpleMembershipProvider or OAuth in ASPNET MVC 4?

I'm using SimpleMembershipProvider in and ASPNET MVC 4 applcation which uses WebApi, Entity Framework 5 and Code First.
I did it following this post, which seeds the database and seems to work.
My question is:
Is there any out of the box admin page to give or remove roles like admin from users?
If not, any suggestion?
Thanks! Guillermo.
No, sadly there isn't. On the bright side, you can create your own and it's designed to work well with EF. Simple Membership is far more flexible than the old .NET Membership.
This is a nice, though not exhaustive overview of simple membership and how to integrate it with EF Code-First.
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
This link guides you through the process of manually creating an admin (near bottom)
http://blog.osbornm.com/2010/07/21/using-simplemembership-with-asp-net-webpages/
In truth, classic Membership is more convenient to use 'out of the box'. setting up 'simpleMembership' is (ironically) heavy compared to the boxed older version. But, after all the OMG, you will probably find it to be a better solution.
SimpleMembership is designed to be customized, unlike previous SqlMembershipProvider versions. As such, an "out of the box" administration tool would not really work because the table layouts would be different from project to project.

Adopting the "aspnet_ ..." sql tables from ASP.NET MVC

I'm just starting a new project on ASP.NET MVC and this will be the first project actually using this technology. As I created my new project with Visual Studio 2010, it created to my sql server a bunch of tables with "aspnet_" prefix. Part of them deal with the built-in user accounts and permission support.
Now, I want to keep some specific information about my users. My question is "Is it a good practice changing the structure of this aspnet_ tables, to meet my needs about user account's information?".
And as i suppose the answer is "No." (Why exactly?), I intend to create my own "Users" table. What is a good approach to connect the records from aspnet_Users table and my own custom Users table.
I want the relationship to be 1:1 and the design in the database to be as transparent as possible in my c# code (I'm using linq to sql if it is important). Also, I don't want to replicate the usernames and passwords from the aspnet_ tables to my table and maintain the data.
I'm considering using a view to join them. Is this a good idea?
Thanks in advance!
EDIT: From the answer, I see that I may not be clear enough, what I want. The question is not IF to use the default asp.net provider, but how to adopt it, to my needs.
I would create custom membership provider and omit those aspnet_x tables completely. I've seen what happens when one joins these tables and custom ones with nhibernate mappings - pure nightmare.
If you are choosing to use the Membership API for your site, then this link has information regarding how to add extra information to a user.
I was faced with the same scenario recently and ended up ditching the membership functionality and rolled my own db solution in tandem with the DotNetOpenAuth library.
Using the membership system in asp.net has its advantages and drawbacks. It's easy to start, because you don't have to worry about validation, user registration, resetting passwords. (Be careful if you plan to modify the table structures, you will have to change them in the views/store procedures generated
However there are drawbacks to using Membership
You will have to maintain 2 separated systems, because the Membership API has restrictions, for example, you cannot perform operations inside a transaction with the membership api. (Unless you use TransactionScope i think, but you don't have other choices).
A valid alternative would be to implement your own security validation routines, and using FormsAuthentication. This way you will have total control over your users tables, and remove dependency to the membership API.

How do I setup a Membership Provider in my existing database using ASP.NET MVC?

For some reason, the idea of setting up Membership in ASP.NET MVC seems really confusing.
Can anyone provide some clear steps to setup the requisite tables, controllers, classes, etc needed to have a working Membership provider?
I know that the Demo that MVC ships with has an Accounts controller. However, should I be using this in my own project? What do I need to get my existing database ready if so? If not, how do I learn what I need to do to implement a membership provider?
Check out this step by step blog on how to set up Membership provider in your asp.net mvc project. The sdk tool you need to get your database ready is aspnet_regsql.exe, you don't need to create a separate database to do that ( a lot of people think they have to provide a separate aspnet.db), you can run the command on your existing database, and it will create the tables, views, and stored procedures to handle the membership provider for you.
However, should I be using this in my
own project? What do I need to get my
existing database ready if so? If not,
how do I learn what I need to do to
implement a membership provider?
The benefit to use the default provider (SqlMembership provider) is to save yourself a lot of time. It involves a lot of work to design a complete membership and role provider.
Edit [2014-06-19] Asp.Net Identity Framework is Microsoft new recommendation to manage user sand permissions.
Check out this link:
https://github.com/TroyGoode/MembershipStarterKit
Most of the work is already done for you. Just download the sample project and run the aspnet_regsql.exe against your database.
check out my answer in this post;
membership
If you want to keep the membership provider that .Net creates for you then you can copy all the tables etc to another sql database and point the provider at it via the config file.
Post a comment if you need more than this.

Resources