ASP.NET标识我的架构

时间:2017-06-10 15:19:43

标签: c# dbcontext asp.net-identity-2

我已经拥有数据库,需要添加ASP.NET身份。我的AspNetUser类:

public partial class AspNetUser
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public AspNetUser()
    {
        AspNetUserClaims = new HashSet<AspNetUserClaim>();
        AspNetUserLogins = new HashSet<AspNetUserLogin>();
        AspNetRoles = new HashSet<AspNetRole>();
    }

    public string Id { get; set; }

    public string UserName { get; set; }

    public string PasswordHash { get; set; }

    public string SecurityStamp { get; set; }

    public int? CompanyId { get; set; }

    public string FullName { get; set; }

    [Required]
    [StringLength(128)]
    public string Discriminator { get; set; }

    public string SMSnumber { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
}

然后我的ApplicationDbContext上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MainContext", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

连接字符串:

<add name="MainContext" connectionString="data source=server;initial catalog=3md_maindb_remote;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

ApplicationUser类:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public int CompanyId { get; set; }
    public string SMSnumber { get; set; }
    public string FullName { get; set; }
}

ApplicationUserManager:

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = false
        };

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false,
        };

        // Configure user lockout defaults
        manager.UserLockoutEnabledByDefault = true;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 5;

        // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
        // You can write your own provider and plug it in here.
        manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
        {
            MessageFormat = "Your security code is {0}"
        });
        manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
        {
            Subject = "Security Code",
            BodyFormat = "Your security code is {0}"
        });
        manager.EmailService = new EmailService();
        manager.SmsService = new SmsService();
        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }
}

当我尝试登录时,出现错误:

  

异常详细信息:System.Data.SqlClient.SqlException:无效列   名称'电子邮件'。列名称“EmailConfirmed”无效。列无效   名字'PhoneNumber'。列名称'PhoneNumberConfirmed'无效。   列名称'TwoFactorEnabled'无效。列名无效   'LockoutEndDateUtc'。列名称“LockoutEnabled”无效。无效   列名'AccessFailedCount'。

另外,我有MainContext:

public partial class MainContext : DbContext
{
    public MainContext()
        : base("name=MainContext")
    {
    }

    public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
    public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
    public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
    public virtual DbSet<AspNetUser> AspNetUsers { get; set; }

据我所知,我需要使用我的MainContext而不是ApplicationDbContext(使用我的架构),但不明白如何......

2 个答案:

答案 0 :(得分:1)

您可以在同一个数据库中使用多个上下文。开箱即用的身份上下文设置为去。如果你不需要改变它,那就不要。仅使用Identity表标识身份。

您无需在身份上下文中定义表。只需添加ApplicationDbContext即可。您可以使用与MainContext相同的连接字符串:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("name=MainContext")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

您真的不应该将所有表添加到IdentityContext,因为它不应该是它们应该存在的位置。虽然这些表可能位于同一个数据库中,但这两个上下文与彼此无关。

在MainContext中保留自己的表,除了可以引用(仅值,而不是数据库引用)IdentityUser到MainContext.User的列之外,不要关联上下文。从MainContext中删除AspNet ...表。

您无法使用AspNetUser类。所以你也可以删除它。请改用ApplicationUser类。要克服缺少字段的问题,请使用:

public class ApplicationUser : IdentityUser
{
    [NotMapped]
    public override bool EmailConfirmed { get => base.EmailConfirmed; set => base.EmailConfirmed = value; }

    // etc.
}

这将忽略缺少的列。请记住,属性存在于代码中并具有默认值。虽然我认为你的代码实际上并不依赖于这些属性。

如果MainContext给您带来麻烦,您可以使用相同的实体对象创建自己的上下文。您可以在同一个数据库上创建多个上下文,包括仅使用的表,即使您使用单独的程序集。

答案 1 :(得分:0)

由于ApplicationDbContext是开箱即用的DbContext。您的项目中不需要任何其他DbContext。因此,只需在项目中完全删除MainContext类,并将自己的实体放在ApplicationDbContext类中。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MainContext", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    // put your extra entities here like this.
    public IDbSet<MyEntity> MyEntites { get; set;}

    // don't put Identity related entities like AspNetRoles. Since already added
}