如何在空的 MVC 4 项目模板中添加 ASP.NET 成员资格提供程序? [英] How to add ASP.NET Membership Provider in a Empty MVC 4 Project Template?

查看:12
本文介绍了如何在空的 MVC 4 项目模板中添加 ASP.NET 成员资格提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ASP.NET MVC4 的新手.我正在创建一个空的 MVC4 项目模板并尝试将 ASP.NET 成员资格提供程序添加到其中,但我不明白我该怎么做.我在 Google 中搜索,但所有演示都使用 Internet 应用程序项目模板.

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.

更新

根据 Nesim Razon 的建议,我将表单身份验证部分从 MVC4 Web 应用程序(Internet 应用程序模板)复制并粘贴到我的空项目中.但现在我得到一个例外

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

要调用此方法,Membership.Provider"属性必须是ExtendedMembershipProvider"的实例.

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();
    }

}

我还在 Web.config 文件中添加了以下指令:

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

<add key="enableSimpleMembership" value="true" /> 

推荐答案

安装

您需要添加以下 Nuget 包:

Installing it

You need to add the following Nuget Packages:

Microsoft.AspNet.WebPages.OAuth

注意:此软件包将自动为您安装所有必需的依赖项.这是已安装的所有 nuget 软件包的详细列表:

Note: This package will install all the required dependencies automatically for you. This is a detailed list of all nuget packages installed:

DotNetOpenAuth.AspNet

DotNetOpenAuth.OAuth.Consumer

添加对

  • 系统事务

现在为了测试它,将以下内容添加到 web.config 文件(在 system.web 下):

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>

添加以下连接字符串

  <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>

我所做的是从 Internet 模板中复制控制器、模型、过滤器和视图,然后修改命名空间等内容,直到您启动并运行它

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

这是结果(没有样式)

登录前

注册

已登录

这篇关于如何在空的 MVC 4 项目模板中添加 ASP.NET 成员资格提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆