C# 如何在空的 MVC 4 项目模板中添加 ASP.NET 成员资格提供程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13324544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:12:18  来源:igfitidea点击:

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

c#asp.net-mvcasp.net-mvc-4membership-provider

提问by Hasanuzzaman

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.

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

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

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

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

要调用此方法,“Membership.Provider”属性必须是“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:

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

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

采纳答案by Jupaol

Installing it

安装它

You need to add the following Nuget Packages:

您需要添加以下 Nuget 包:

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

现在为了测试它,将以下内容添加到 web.config 文件(下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

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

This is the result (without styles)

这是结果(没有样式)

Before login

登录前

enter image description here

在此处输入图片说明

Registering

注册

enter image description here

在此处输入图片说明

Logged IN

登录

enter image description here

在此处输入图片说明

回答by Bhavesh Chauhan

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 templateand copy Account controller + views and also update web.config with correct connectionString and Forms Auth section and you should be good to go.

如果您想使用 Universal Providers 而不是包含在 MVC 4 Internet 模板中的 Simple Membership,您可以使用Mobile 模板创建一个新的 MVC 4 项目并复制 Account controller + 视图,并使用正确的 connectionString 和 Forms Auth 部分更新 web.config你应该很高兴去。

回答by nasa8x

Simple Membership in ASP NET MVC 5 Web Applications

ASP NET MVC 5 Web 应用程序中的简单成员资格

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

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.

一旦项目开发,在项目中实现用户身份验证是非常困难的。在本视频中,我演示了如何在那些从一开始就没有实施身份验证的项目中有效地利用 Microsoft 简单成员身份功能。借助此功能,您可以在项目的任何阶段实施身份验证功能。