Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - How to add ASP.NET Membership Provider in a Empty MVC 4 Project Template?

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" /> 
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...