I've tried for 2 days to get something working but so far it's been pointless. What I need is to manage users and their roles, nothing else.
What I've tried is to understand how to do it in MVC but I can't seem to find a detailed tutorial on how to do it, either on the internet or in any MVC 4 book.
I started by enabling the aspnet database with the aspnet_regsql utility, this create these tables:
That's all I found on the tutorial, it does not show how to interact with these tables (which I assume is with the default membership provider), so far I've found there are 3 main options for doing this:
I found this tutorial:
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-7
Here they ask to create a new MVC 4 application project with internet application template and copy all the files relating the account control.
I did that for my project but as soon as I try to log in I get :
To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".
which according to this forum: http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/d352bb1b-577c-42b7-8872-5ed59cd65f32/
is because of how I defined the profile, membership and roleManager providers on my web.config file which is defined as follows:
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DatabaseConnection" applicationName="/" />
</providers>
</profile>
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DatabaseConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="DatabaseConnection" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
All I need is to be able to add new users, assign roles to them and show different content according to the role accessing the view.
Question #1 What is the membership provider being used when I create an MVC 4 application with an internet application template?
Question #2
What am I doing wrong with the web.config file? How can I define the providers correctly?
Question #3
Which option suits me better? If you know a nice tutorial for it could you please link me to it?
Any help will be really appreciated! thanks in advance!
Membership provider you are using is dated. New Universal Membership Provider uses new table names without prefix aspnet_
.
1) Please take a look at Scott Hanselman's link.
2) You do not need profile
if you do not use it. roleManager
configuration is not correct.
FYI: Please define machineKey
explicitly in web.config; it will save you a lot of headache in the future.
P.S. I agreed with you. I just finished reading Pro ASP.NET MVC 4 by Adam Freeman
; he did not mention about Membership Provider. ASP.NET MVC 4 and the Web API by Jamie Kurtz
uses legacy Membership Provider if you want to read.
Question #1 What is the membership provider being used when I create an MVC 4 application with an internet application template?
Answer: SimpleMembershipProvider
Question #2 What am I doing wrong with the web.config file? how can I define the providers correctly?
Answer: For SimpleMembershipProvider, modifications to the web.config file are not necessary. See here for detailed configuration/setup information and tutorial: http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html
Membership and security was extremely overcomplicated in ASP.Net and is still overcomplicated in ASP.NET MVC. But fortunately it's possible to not use built-in membership at all. I've created simple solutione and used in a lot of projects and now added it to github see http://github.com/TesserisPro/ASP.NET-SImple-Security-Provider You can easy customize it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With