简体   繁体   English

ASP.NET MVC中的注册用户数据库

[英]Database of registered users in ASP.NET MVC

Simple question. 简单的问题。 I know that in the default MVC application in Visual Studio, you can register a user. 我知道在Visual Studio的默认MVC应用程序中,您可以注册用户。 So I want to be able to put that info in a database and use ADO.net or LINQ to SQL to be able to communicate. 因此,我希望能够将该信息放入数据库中,并使用ADO.net或LINQ to SQL进行通信。 How do I do this? 我该怎么做呢?

Do you have a database or do you want to create a new one? 您有数据库还是要创建一个新数据库? If you have one just create a connection in server exploer, add new Model in Visual studio, select LinqToSql Classes, select tables from your connection and drag and drop the tables you need to the design surface. 如果您只是在服务器浏览器中创建一个连接,则在Visual Studio中添加新模型,选择LinqToSql类,从连接中选择表,然后将所需的表拖放到设计图面上。 Then you can access your model from your controller where the user data is posted to. 然后,您可以从发布用户数据的控制器访问模型。 Check this thread for example of saving. 检查此线程以获取保存示例。

If you don't have a database you obviously have to design and create it first 如果您没有数据库,显然必须先设计和创建它

Some might argue, but you probably do not want to use the built-in membership provider. 有人可能会说,但你可能希望使用内置的成员资格提供。 The reason is that it generates its own default schema which is good to start with, but it's better to just bite the bullet and design your own. 原因是它会生成自己的默认模式,这是很好的开始,但是最好是硬着头皮设计自己的模式。 That way, you're in control of your application, and do not have to migrate your database whenever some change is required. 这样,您就可以控制应用程序,并且不必在需要任何更改时就迁移数据库。

The sample stuff in ASP.NET MVC uses the built-in membership and forms authentication in ASP.NET. ASP.NET MVC中的示例内容使用内置成员身份并在ASP.NET中进行表单身份验证。 In the Web.config, you can configure it to store the data in SQL Server rather than the default which I think might is SQLExpress. 在Web.config中,您可以将其配置为将数据存储在SQL Server中,而不是默认的SQLExpress中。 MSDN has an article on how to do this: MSDN上有一篇有关如何执行此操作的文章:

How To: Use Forms Authentication with SQL Server in ASP.NET 2.0 如何:在ASP.NET 2.0中的SQL Server中使用表单身份验证

You basically just run a sql script and it generates a bunch of tables and stored procedures in whatever database you are already using. 基本上,您只需要运行一个sql脚本,它就会在您正在使用的任何数据库中生成一堆表和存储过程。 You can then access the database directly or through the membership API. 然后,您可以直接或通过成员资格API访问数据库。

Alternatively, you can roll your own implentation of IMembershipService which just has a few methods to handle: 或者,您可以自己动手执行IMembershipService,该方法只有几种方法可以处理:

public interface IMembershipService
{
    int MinPasswordLength { get; }

    bool ValidateUser(string userName, string password);
    MembershipCreateStatus CreateUser(string userName, ...);
    bool ChangePassword(string userName, ...);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM