ASP MVC 用户配置文件

发布于 2024-10-01 02:10:13 字数 459 浏览 0 评论 0原文

我以前做过MVC,但我对ASP 和ASP MVC 很陌生。到目前为止,我真的很喜欢 ASP MVC 为我提供的便利,但我无法弄清楚如何更好地控制用户。默认情况下,MVC 提供最小的用户注册表单。我环顾四周,但仍然有两个问题:

  1. 如何使用户数据库成为我的项目中的本地数据库?我认为 SQLEXPRESS 用于存储用户值,这似乎是一个神奇的过程。我该如何消除这种魔力?我想对该数据库的位置有更多的控制。

  2. 这引出了另一个问题:如何扩展用户?我一直在阅读个人资料,但我仍然对一些事情感到困惑。如何准备个人资料并将其与用户链接?什么作为外键?而且,在我的控制器中,我如何访问用户的各个部分,例如用户名、电子邮件,甚至从个人资料中访问名字、姓氏(尽管我猜想,当我在本地拥有个人资料数据库和用户数据库时,我可以运行sql 命令来检索数据)

我真的很感激一些指向正确资源的指针,和/或 ASP.NET 的最佳实践

I've done MVC in the past, but I am new to ASP and ASP MVC. I really love the ease that ASP MVC provides me, so far, but I am having trouble figuring out how to get more control over the Users. By default, the MVC provides a minimal user registration form. I have looked around quite a bit, but I still have two questions:

  1. How do I make the User data base a local database in my project? I think SQLEXPRESS is used to store the user values, in what seems like a magical process. How do I de-magic-ify this? I would like to have more control on the location of this database.

  2. This leads to another question: How do I expand the User? I have been reading up on Profiles, but I am still confused about a few things. How do I prepare a Profile and link it with a User? What serves as the foreign key? And, in my controllers, how can I access various parts of the user like username, email, or even from the profile stuff like firstname, lastname (though I guess once when I have a Profile's database and a User's database locally, I can run sql commands to retrieve data)

I would really appreciate some pointers to the right resources, and/or best practices with ASP.NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-10-08 02:10:14

我首先阅读这篇关于扩展的官方 Microsoft 文章 ASP.NET 会员 API。它讨论了创建额外的表来存储有关用户的附加信息。

会员数据库

  1. 如果您有一个现有数据库,其中包含所有其他网站信息,您可以运行aspnet_regsql.exe 工具来生成必要的用户表。然后,您需要修改 web.config 并添加 SqlMembershipProvider 以及连接字符串。
  2. 如果您要创建一个新项目并且没有数据库,请从一个已启用成员资格的新 MVC 项目开始。首次使用时,您的数据库将在 App_Data 文件夹内创建,您可以将其附加到 SQL/SQLEXPRESS 服务器。然后只需更改连接字符串以使用数据库服务器而不是本地文件即可。

创建附加表

这部分实际上非常简单,由几个简短的步骤组成:

  1. 创建一个新表,即 UserProfiles
  2. 添加一个 uniqueidentifier 列作为主键,并添加一个aspnet_Users 表的外键
  3. 添加要存储的任何其他字段(电话、地址、性别等)。
  4. 如果您使用 LINQ-to-SQL 或实体框架,则只需将所需的表拖到设计器上即可,您就可以查询会员表了。

这是一个关于使用的小示例

将此代码段添加到负责配置文件/帐户信息的存储库中。

public aspnet_User GetUser()
{
    MembershipUser user = Membership.GetUser();
    return db.aspnet_Users.SingleOrDefault(u => u.UserId == user.ProviderUserKey);
}

然后在模型中,您可以获取用户并访问其他用户存储在 UserProfiles 表中的信息。

AccountRepo accountRepo = new AccountRepo();
aspnet_User user = accountRepo.GetUser();
string Address = user.UserProfile.Address; // bingo!

差不多就是这样!

这显然是一个简单的示例,您应该检查用户是否为 null > 您还可以创建一个类,负责返回有关用户的必要信息、实现缓存等。

I would start by reading this official Microsoft article on extending the ASP.NET Membership API. It talks about creating extra tables for storing additional information about users.

The membership database

  1. If you have an existing database which holds all your other website information, you can run the aspnet_regsql.exe tool to generate the necessary user tables. You will then need to modify your web.config and add the SqlMembershipProvider along with your connection string.
  2. If you're creating a new project and don't have a database, start with a new MVC project which already has Membership enabled. Your database will be created inside the App_Data folder on first use, and you can take this and attach it to your SQL/SQLEXPRESS server. Then it's just a matter of changing the connection string to use a DB server rather than a local file.

Creating additional tables

This part is actually quite simple and consists of a few short steps:

  1. Create a new table, i.e. UserProfiles
  2. Add a uniqueidentifier column as your primary key, and add a foreign key to the aspnet_Users table
  3. Add any other fields you want to store (Phone, Address, Gender etc.)
  4. If you're using LINQ-to-SQL or the Entity Framework, you can simply drag the tables you need onto the designer, and you'll be ready to query the Membership tables.

Here's a little sample on usage

Add this snippet to your repository responsible for Profile/Account information.

public aspnet_User GetUser()
{
    MembershipUser user = Membership.GetUser();
    return db.aspnet_Users.SingleOrDefault(u => u.UserId == user.ProviderUserKey);
}

Then inside your models, you can get the user and access the other information stored in your UserProfiles table.

AccountRepo accountRepo = new AccountRepo();
aspnet_User user = accountRepo.GetUser();
string Address = user.UserProfile.Address; // bingo!

And that's pretty much it!

This is obviously a simple example, and you should be checking if the user is null and you could also create a class responsible for returning the necessary information about a user, implement caching, etc..

情感失落者 2024-10-08 02:10:14

我将从这里开始:

另外还有一个很棒的文章系列(18 篇文章!!!)来自 Scott米切尔 4GuysFromRolla

ASP.NET 成员资格模型被设计为具有可插入的体系结构。您可以编写自己的最适合您需求的 MembershipProvider 实现。

即使您在网上找到的大多数示例都涉及 ASP.NET Web 表单,但与 MVC 一起使用时也只有非常小的差异。

I would start from here:

Also a great article series (18 articles!!!) is from Scott Mitchell at 4GuysFromRolla.

The ASP.NET membership model is desgned to have a pluggable architecture. You can write you own MembershipProvider implementation that best suit your needs.

Even if most of the samples you will find on the net regards ASP.NET web forms, there are only very small differences when used with MVC.

淡淡绿茶香 2024-10-08 02:10:14

如果您仍在寻求对此的深入了解,我刚刚发现在 MVC 4 WebPages 站点中,有一个名为 SimpleMembership 提供程序的提供程序。它为开发人员提供了对网站上存储的用户、角色和会员信息的更多控制。更多这里:
http://blog.osbornm .com/archive/2010/07/21/using-simplemembership-with-asp.net-webpages.aspx

If you're still looking for insight into this, I just ran across the fact that in MVC 4 WebPages sites, there's a provider called the SimpleMembership provider. It gives more control to the developer of the Users, Roles and Membership info stored on websites. More here:
http://blog.osbornm.com/archive/2010/07/21/using-simplemembership-with-asp.net-webpages.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文