加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何自定义简单的成员资格提供者来处理我自己的

发布时间:2020-12-15 23:06:00 所属栏目:asp.Net 来源:网络整理
导读:这几天我正在探索ASP.NET MVC 4.如果有人可以回答我的问题,我会很高兴. 我正在建设一个学术项目“项目管理与支持体系”.我设计了我自己的数据库,我的数据库中有用户的表(两种用户:将执行任务的员工,以及分配/租用任务的客户端),我正在创建一个新的会员提供
这几天我正在探索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-现在,我正在同步这个表与我自己的数据库中的用户的表.考虑到我添加了另一个“帐户类型”字段请求用户注册时创建一个特定的配置文件.

任何帮助非常感谢.

干杯

解决方法

使用SimpleMembership有两种存储和使用该信息进行身份验证的方法.

>您可以使用默认(UserProfiles)表,即在“DefaultConnection”字符串指向的数据库中.
>您可以使用您的数据库和其中的表来替代默认的UserProfiles表.

选项1在其他地方很好地解释.选项2按照下列步骤:
假设您的数据库上下文是mDbContext,并且您要用于替换UserProfiles的表是Employees.

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

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);

>在AccountModel中的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注册方法中为HttpPost替换

WebSecurity.CreateUserAndAccount(model.UserName,model.

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

>如果挂起任何更改(或添加迁移),则重建和更新数据库.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读