如何自定义简单成员资格提供程序以与我的数据库一起使用ASP.NET MVC 4?

12

最近我在探索ASP.NET MVC 4。若有人能回答我的问题,我将非常高兴。

我正在构建一个名为“项目管理和支持系统”的学术项目。我已经设计了自己的数据库,并在其中为用户创建了自己的表格(两种类型的用户:执行任务的员工和分配/雇用任务的客户)。我原本想要创建一个新的成员资格提供程序,但我意识到“重复造轮子是浪费时间”。

现在,我正在使用SimpleMembership(它是MVC应用程序的成员服务的未来)构建ASP.NET MVC4上的成员模型。它为ASP.NET框架提供了更简洁的成员提供程序,并额外支持OAuth。

1- 我创建了一个开箱即用的ASP.NET MVC 4互联网应用程序,以定制登录、注册和用户管理逻辑来维护用户个人资料表。我添加了三个角色:管理员、员工和客户。

通过这篇博客文章,我可以自定义注册http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

2- 现在,我正在努力将这个表格与我自己数据库中的用户表格同步。考虑到我添加了另一个名为“帐户类型”的字段,要求用户在注册时创建一个特定的个人资料。

非常感谢任何帮助。

祝好


看一下这个入门套件:http://github.com/kriasoft/site-sdk 它有一个自定义的会员解决方案。 - Grief Coder
2个回答

26

使用SimpleMembership进行身份验证有两种存储和使用信息的方式。

  1. 您可以使用默认的(UserProfiles)表,该表位于“DefaultConnection”字符串指向的数据库中。

  2. 您可以使用您自己的数据库和其中的一个表来替换默认的UserProfiles表。

选项1在其他地方已经非常好地解释了。 对于选项2,请按照以下步骤进行操作:

假设您的数据库上下文为mDbContext,并且要用作替换UserProfiles的表为Employees。

  1. 您的Employee模型如下所示:

namespace m.Models
{
  public class Employee
  {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string UserName { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Mobile { get; set; }
    public Designation Designation { get; set; }
    .........
  • 你的 DbContext 看起来像这样

  • namespace m.Models
    {
        public class mDBContext : DbContext
        {
             DbSet<Employee> Employees { get; set; }
    ......
    
  • 你需要告诉WebSecurity使用你的数据库。

  • WebSecurity.InitializeDatabaseConnection("mDBContext", 
      "Employees", "ID", "UserName", autoCreateTables: true);
    
  • 在AccountModels中的RegisterModel类中添加额外的字段

  • public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }
    
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    
        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Mobile { get; set; }
        public Designation Designation { get; set; }
    }
    
    在 AccountController 的 Register 方法中,将 HttpPost 替换为

    WebSecurity.CreateUserAndAccount(model.UserName, model.
    
    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, 
      new {  FirstName = model.FirstName, LastName = model.LastName, 
             Mobile = model.Mobile});
    
  • 如果有任何更改需要处理(或添加迁移),请重建并更新数据库。


  • 1
    +1 这似乎是 ASP.NET 文档中缺少的内容。 - jingtao

    0

    我一个小时前也在查看同一篇文章,但是没有解决问题。请问我应该从项目的哪个部分将用户资料保存到我的数据库中(我的意思是应该使用哪个类将用户资料从用户资料中注入到我的数据库中)! - ImnotaGeek
    谢谢!我现在正处于这个阶段,我所构建的一切都类似于此处分享的代码。http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/ - ImnotaGeek
    @ImnotaGeek 如果你想控制并将SimpleMembership包含在你的数据库中,那么你正在跟踪的链接是没有帮助的。再次查看这个链接,并在你卡住的地方提问:http://blog.spontaneouspublicity.com/including-asp-net-simple-membership-tables-as-part-of-your-entity-framework-model - Komengem
    谢谢!我已经阅读了这篇文章,成功地应用了所有步骤。在注册后,我应该插入数据吗(我对MVC不是很熟悉),我应该修改哪个类? - ImnotaGeek
    谢谢!我想我找到了我要找的东西:https://dev59.com/fW_Xa4cB1Zd3GeqPxiKm - ImnotaGeek
    显示剩余2条评论

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接