如何自定义简单的成员提供我自己的数据库ASP.NET MVC 4工作 [英] How can I customize simple membership provider to work with my own database ASP.NET mvc 4

查看:265
本文介绍了如何自定义简单的成员提供我自己的数据库ASP.NET MVC 4工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我探索ASP.NET MVC 4这些天。我会很高兴,如果有人可以回答我的问题有所帮助。

I’m exploring ASP.NET MVC 4 these days. I will be pleased if someone can help by answering my question .

我建立一个学术项目项目管理与支持系统。我设计我自己的数据库,我有我自己对我的数据库用户表(二种用户:员工谁将会执行任务,客户谁指派/聘请的任务),我正要创建一个新的成员提供程序,但我认识到,这是浪费时间 - 重新发明轮子。

I am building an academic project "Project management and support system" . I have designed my own database , I have my own tables for the users in my database (two kinds of users : Employee who will execute tasks , and a client who assign/hire for tasks ) ,I was about creating a new membership provider but I realized that "It's a waste of time - reinventing the wheel".

现在,我在ASP.NET MVC4使用SimpleMembership(它的会员服务,为MVC应用的未来)。它提供了一个更简洁的成员提供的ASP.NET框架,并支持OAuth的另外,建立会员制模式

Now , I am building a membership model on ASP.NET MVC4 using SimpleMembership (It's the future of membership services for MVC applications ).It provides a more concise membership provider to the ASP.NET framework, and supports OAuth additionally.

1,我创建一个不折不扣的现成ASP.NET MVC 4互联网应用自定义登录,注册和用户管理逻辑,以保持用户的个人资料表。
我加了三个角色:管理员,员工,客户端

1- I created an out-of-the-box ASP.NET MVC 4 internet application to customize the Login, Signup and User management logic to maintain the user-profile table. I added three roles : Admin , Employee , Client

通过这个博文去,我能够定制注册<一个href=\"http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-$c$cfirst-and-custom-user-properties/\">http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-$c$cfirst-and-custom-user-properties/

Going through this blogpost , I am able to customize the registration http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

2 - 现在,我的工作与我在我自己的数据库用户的表同步此表。也要考虑到我已经加入的账户类型在注册时要求用户创建一个特定的配置文件的另一领域。

2- Now , I am working synchronize this table with the tables of the users that I have in my own database . Taking in consideration that I have added another field of "Account Type" Asking the user during the registration to create a specific profile .

任何帮助非常AP preciated。

Any help greatly appreciated.

干杯

推荐答案

与SimpleMembership有2种方式来存储和使用该信息进行验证。

with SimpleMembership there are 2 ways for storing and using that information for authentication.


  1. 您可以使用默认值(的UserProfiles)表,即在数据库中DefaultConnection指向的字符串。

  1. you may use default (UserProfiles) table, that is in database pointed to by "DefaultConnection" string.

您可以使用您的数据库,其中用作替换默认的UserProfiles表的表。

you may use YOUR database and a table therein to be used as replacement of default UserProfiles table.

选项1别处很好的解释。选项​​2的后续步骤如下:
假设你的数据库上下文是mDbContext和表要用于替换的UserProfiles的是员工。

option 1 is explained very well elsewhere. for option 2 follow steps given below: suppose your database context is mDbContext and table that you want to use for replacement of UserProfiles is Employees.


  1. 您的员工的模型看起来像这样

  1. your Employee model looks like this

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看起来像这样

  • your DbContext looks like this

    namespace m.Models
    {
        public class mDBContext : DbContext
        {
             DbSet<Employee> Employees { get; set; }
    ......
    


  • 你需要告诉WebSecurity使用你的数据库。

  • you need to tell WebSecurity to use your database.

    WebSecurity.InitializeDatabaseConnection("mDBContext", 
      "Employees", "ID", "UserName", autoCreateTables: true);
    


  • 加入RegisterModel类的附加字段中AccountModels

  • add additional fields in RegisterModel class in AccountModels

    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注册方法HttpPost替换

  • In AccountController Register method for HttpPost replace

    WebSecurity.CreateUserAndAccount(model.UserName, model.
    

    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, 
      new {  FirstName = model.FirstName, LastName = model.LastName, 
             Mobile = model.Mobile});
    


  • 重建和更新数据库,如果挂起的任何更改(或添加迁移)。

  • rebuild and update database if pending any changes (or add migrations).

    这篇关于如何自定义简单的成员提供我自己的数据库ASP.NET MVC 4工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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