如何使用 SimpleMembership 管理个人资料? [英] How do I manage profiles using SimpleMembership?

查看:21
本文介绍了如何使用 SimpleMembership 管理个人资料?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Internet 模板的 ASP.NET MVC 4 站点.我正在使用使用该模板设置的 SimpleMembership.

I have an ASP.NET MVC 4 site based off the internet template. I am using the SimpleMembership which i set up with that template.

我可以修改为我创建的用户表,但我不确定修改我添加的额外字段的正确"方法.我想要全名、电子邮件等并将它们添加到用户表中,但似乎无法通过 SimpleMembership WebSecurity.* 静态方法进行更新.

I can modify the Users table which has been creted for me but I am unsure as to the "correct" way to modify the extra fields I have added. I want Fullname, Email etc and have added them to the user table but there appears no way to update through the SimpleMembership WebSecurity.* static methods.

您是否应该在 SimpleMembership API 之外使用 EF 自己更新这些属性?

Are you supposed to just update those properties yourself using EF outside of the SimpleMembership API?

推荐答案

1 - 您需要启用迁移,最好使用 EntityFramework 5

1 - You need to enable migrations, prefereably with EntityFramework 5

2 - 移动你的

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "EmailAddress", autoCreateTables: true); 

到 YourMvcApp/Migrations/Configuration.cs 类中的 Seed 方法

to your Seed method in your YourMvcApp/Migrations/Configuration.cs class

    protected override void Seed(UsersContext context)
    {
        WebSecurity.InitializeDatabaseConnection(
            "DefaultConnection",
            "UserProfile",
            "UserId",
            "UserName", autoCreateTables: true);

        if (!Roles.RoleExists("Administrator"))
            Roles.CreateRole("Administrator");

        if (!WebSecurity.UserExists("lelong37"))
            WebSecurity.CreateUserAndAccount(
                "lelong37",
                "password",
                new {Mobile = "+19725000000", IsSmsVerified = false});

        if (!Roles.GetRolesForUser("lelong37").Contains("Administrator"))
            Roles.AddUsersToRoles(new[] {"lelong37"}, new[] {"Administrator"});
    }

现在 EF5 将负责创建您的 UserProfile 表,执行此操作后,您将调用 WebSecurity.InitializeDatabaseConnection 仅向已创建的 UserProfile 表注册 SimpleMembershipProvider,同时告诉 SimpleMembershipProvider 哪一列是 UserId 和 UserName.我还展示了一个示例,说明如何在 Seed 方法中添加用户、角色并将两者与 自定义 UserProfile 属性/字段相关联,例如用户的手机(号码)和 IsSmsVerified.

Now EF5 will be in charge of creating your UserProfile table, after doing so you will call the WebSecurity.InitializeDatabaseConnection to only register SimpleMembershipProvider with the already created UserProfile table, also tellling SimpleMembershipProvider which column is the UserId and UserName. I am also showing an example of how you can add Users, Roles and associating the two in your Seed method with custom UserProfile properties/fields e.g. a user's Mobile (number) and IsSmsVerified.

3 - 现在,当您从包管理器控制台运行 update-database 时,EF5 将为您的表提供所有自定义属性

3 - Now when you run update-database from Package Manager Console, EF5 will provision your table with all your custom properties

有关其他参考资料,请参阅带有源代码的这篇文章:http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleoleprovider-ef5-codefirst-and-custom-user- 属性/

For additional references please refer to this article with sourcecode: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

这篇关于如何使用 SimpleMembership 管理个人资料?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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