微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 如何自定义简单的成员资格提供者来处理我自己的数据库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});

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

原文地址:https://www.jb51.cc/aspnet/249984.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐