How to add ASP.NET Membership Provider in a Empty MVC 4 Project Template?
Asked Answered
K

4

23

I am new in ASP.NET MVC4. I am creating a Empty MVC4 Project Template and trying to add ASP.NET Membership Provider into it but i am not understanding how can I do it. I am searching in Google but all demos are using Internet Application project template.

I know this question is not good but i am already spend two days for it.

Please give a some advice or tutorial for this purpose.

Update

According to Nesim Razon`s advice i copy and paste forms authentication sections to my empty project from a MVC4 Web Application (Internet Application template). But now i get an exception

To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".

[InitializeSimpleMembership]
public class HomeController : Controller
{

    public ActionResult Index()
    {
        WebSecurity.CreateUserAndAccount("Name", "Password"); // Exception is thrown from there.
        return View();
    }

}

I am also added the following directive in the Web.config file:

<add key="enableSimpleMembership" value="true" /> 
Kilmarnock answered 10/11, 2012 at 17:52 Comment(7)
why don't you create an MVC4 Web Application (Internet Application template) than copy paste related forms authentication sections to your empty project?Trowbridge
Are you trying to use the built-in providers, or are you trying to add custom providers?Jumble
I am trying to use built-in provider which is use in MVC4 Web Application (Internet Application template)Kilmarnock
That would be SimpleMembership, which has some dependencies that are a bit of a pain to resolve in an empty project. Why not just create a default web project and remove what you're not using?Jumble
Can you tell me what are the dependencies?Kilmarnock
Just to clarify what provider do you want to use SimpleMembershipProvider (to use things like OAuth) or the traditional legacy SqlMembershipProvider??Nonsectarian
i want to use SimpleMembershipProvider.Kilmarnock
N
31

Installing it

You need to add the following Nuget Packages:

Add a reference to

  • System.Transactions

Testing it

Now in order to test it, add the following to the web.config file (Under system.web):

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

Add the following connection string

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-UnitTestQueries.UI-20121105001038;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-UnitTestQueries.UI-20121105001038.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

And what I did was to copy the controllers, models, filters and views from the Internet template and just modify things like the namespace and so on until you get it up and running

This is the result (without styles)

Before login

enter image description here

Registering

enter image description here

Logged IN

enter image description here

Nonsectarian answered 10/11, 2012 at 20:39 Comment(0)
M
2

Check out SimpleMembership, Membership Providers, Universal Providers and the new ASP.NET 4.5 Web Forms and ASP.NET MVC 4 templates to get an info about what you need to set SimpleMembership inside empty template.

You my also want to check out very nice posts from Scott Allen:

Perils of the MVC4 AccountController

Build Your Own Membership System For ASP.NET MVC - Part I

and

Build Your Own Membership System For ASP.NET MVC - Part II

Modification answered 10/11, 2012 at 20:22 Comment(0)
S
0

If you want to use Universal Providers as opposed to Simple Membership that's included in MVC 4 Internet template, you can create a new MVC 4 project using Mobile template and copy Account controller + views and also update web.config with correct connectionString and Forms Auth section and you should be good to go.

Servomotor answered 12/11, 2012 at 4:17 Comment(0)
L
0

Simple Membership in ASP NET MVC 5 Web Applications

https://www.youtube.com/watch?v=BsKcVbsMn6w

Implementing User Authentication in a project once it is developed is really difficult. In this video I have demonstrated how you can effectively utilize Microsoft Simple Membership feature in those projects in which you haven't implemented the Authentication from the very start. With the help of this you can implement the authentication feature at any stage of the project.

Lanoralanose answered 12/11, 2014 at 3:11 Comment(2)
Since the OP specified MVC 4, is this still applicable? Please specify any differences required from the process you have in this video, or address the compatibility directly.Lobscouse
The link is dead!Vesicatory

© 2022 - 2024 — McMap. All rights reserved.