asp.net-mvc 如何将 ASP.NET Identity 实现到一个空的 MVC 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22273362/
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-09-08 03:34:44  来源:igfitidea点击:

how to implement ASP.NET Identity to an empty MVC project

asp.net-mvcasp.net-identity

提问by Kamran Sadin

I create a new empty MVCproject and I want to add identity to it. Now I do not know how to do this and how to create database and tables and classes. I searched but I did not find any useful solution for my question, I found an article for adding ASP.NET Identity to an existing empty ASP.NET Web from but for ASP.Net MVCproject I did not find.
for log in
for registration form
for adding a user to role
for deleting user
how to create tables in my own database
how to manage users

我创建了一个新的空MVC项目,并想为其添加身份。现在我不知道如何做到这一点以及如何创建数据库、表和类。我进行了搜索,但没有找到对我的问题有用的解决方案,我找到了一篇文章,用于将 ASP.NET Identity 添加到现有的空 ASP.NET Web 中,但是对于 ASP.NetMVC项目我没有找到。
用于登录
用于注册
用于添加用户到角色
用于删除用户
如何在我自己的数据库中创建表
如何管理用户

回答by Ganhammar

回答by jbutler483

Open up the nuget package manager by going to:

通过以下方式打开 nuget 包管理器:

Tools > Nuget Package Manager > Package Manager Console

in a new project[1]. From there, You can enter next to the PM>

在一个新项目中[1]。从那里,您可以进入PM>

Install-Package Microsoft.AspNet.Identity.Samples -Pre 

which will install the Identity sample for you. It will ask you if you allow it to alter your webconfigby pressing 'A' you accept all changes.

这将为您安装 Identity 示例。它会询问您是否允许它webconfig通过按“A”来更改您的所有更改。

From then onwards, you'll have the sample identity project installed!

从那时起,您将安装示例身份项目!

Note: It will change the default namespace, so by going to 'find and replace' you can quickly alter the occurrences to your project name (note, you may have to go into your global.aspxpage in your my documents folder - open it in notepad and change the namespace there too!)

注意:它会更改默认命名空间,因此通过“查找和替换”,您可以快速更改项目名称的出现次数(注意,您可能需要进入global.aspx我的文档文件夹中的页面 - 在记事本中打开它并在那里也更改命名空间!)



[1]: If you do not do this at the beginning of your development, you will find that the following files will be overwritten(so make sure you have a way to merge your originals with the new):

[1]:如果你在开发之初不这样做,你会发现以下文件会被覆盖(所以确保你有办法将你的原件与新的合并):

Overwrite existing file 'Views\Web.config'.
Overwrite existing file 'Views\Shared\Error.cshtml'.
Overwrite existing file 'Views\Shared\_Layout.cshtml'.
Overwrite existing file 'Views\Home\Index.cshtml'.
Overwrite existing file 'Views\_ViewStart.cshtml'.
Overwrite existing file 'Global.asax.cs'.
Overwrite existing file 'Global.asax'.
Overwrite existing file 'Controllers\HomeController.cs'.
Overwrite existing file 'Content\Site.css'.
Overwrite existing file 'App_Start\RouteConfig.cs'.
Overwrite existing file 'App_Start\FilterConfig.cs'.
Overwrite existing file 'App_Start\BundleConfig.cs'.

回答by Sandeep Shekhawat

You can integrate ASP.NET Identity from the NuGet gallery. You can install these packages using the NuGet Package Manager Console, like this:

您可以从 NuGet 库中集成 ASP.NET Identity。您可以使用 NuGet 包管理器控制台安装这些包,如下所示:

Install-Package Microsoft.AspNet.Identity.EntityFramework –Version 2.2.1

安装包 Microsoft.AspNet.Identity.EntityFramework –Version 2.2.1

Install-Package Microsoft.AspNet.Identity.Core -Version 2.2.1

安装包 Microsoft.AspNet.Identity.Core -Version 2.2.1

Install-Package Microsoft.AspNet.Identity.OWIN -Version 2.2.1

安装包 Microsoft.AspNet.Identity.OWIN -Version 2.2.1

Apart from that, you just create a separate MVC project with the default template and where you have classes and methods for implementation of ASP.NET identity as a reference so just use these classes in your new project as well.

除此之外,您只需使用默认模板创建一个单独的 MVC 项目,并在其中提供用于实现 ASP.NET 标识的类和方法作为参考,因此也只需在新项目中使用这些类。

If you are using existing database with Entity Framework with Database first approach then create the separate connection string and data context for ASP.NET identity.As Entity Framework with Edmx uses the provider System.Data.EntityClientwhile ASP.NET Identity uses the provider System.Data.SqlClient.

如果您将现有数据库与 Entity Framework 与 Database first 方法一起使用,System.Data.EntityClient则为 ASP.NET 标识创建单独的连接字符串和数据上下文。因为 Entity Framework with Edmx 使用提供程序,而 ASP.NET Identity 使用提供程序System.Data.SqlClient

回答by pranav rastogi

The following article explains you the basics of adding ASP.NET Identity to your application. http://www.asp.net/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project

下面的文章向您介绍了将 ASP.NET 标识添加到应用程序的基础知识。http://www.asp.net/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project