How to configure your own schema for the new Microsoft.Asp.NET Identity (MVC5)

I struggled with the new Forms Identity process that Microsoft introduced with the implementation of One Asp.Net, specifically with regard to Asp.NET MVC5.

My problems are twofold:

  • How to point context to a regular instance of SQL instead of LocalDb. At least LocalDb is not viable for me, as my hosting provider does not support it.

  • How to use a user database for my authentication objects.

I understand that I can just change the connection string to write "DefaultConnection" in Web.Config. But it still leaves me with a number of tables with the "AspNet" prefix or some of them.

In the old MembershipProvider architecture, you can inherit from providers and implement everything you need in any way by placing entries in Web.config that pointed out the forms authentication process to your custom membership process.

However, when using the new authentication process, there are no hooks in the web.config file. In addition, it is not so good to inherit from it as nothing seems virtual and, first of all, disconnects it; it is inherited from the existing context for loading.

I tried to use my own context with objects that implement the corresponding new interfaces, but all he did was completely ignore my context, and not only that, use localdb with the default connection ConnectionConnection String, ignoring my own context.

Since this is Microsoft’s new brainchild, there are no real approaches to setting up a new process, just a demo of how big the new process is.

I came across one entry on the Internet that talked about deploying your own authentication process and connecting it to the OWIN process, but I am completely unfamiliar with OWIN and need at least some information on how to do this. (My initial foray into the OWIN project yields no results)

Does anyone have any info?

+7
c # asp.net-mvc asp.net-identity
source share
1 answer

Update: I completely rewrite my answer because I learned a lot more about this process and realized that my answer was almost not complete enough.

At first . I created my entities that inherit from IdentityXXX Identity classes (for example only):

using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using NTier.Web.Core.Interfaces.Common; using NTier.Web.Core.Interfaces.DataModels; using NTier.Web.Core.Interfaces.Stores; namespace NTier.Web.DataAccess.Entities { public sealed class MemberEntity : IdentityUser<Guid, MemberLogin, MemberRole, MemberClaim>, IMemberDataModel, IAuditable { public MemberEntity() { Id = Guid.NewGuid(); } #region Overrides of IdentityUser<Guid,MemberLogin,MemberRole,MemberClaim> public override Guid Id { get { return base.Id; } set { base.Id = value != Guid.Empty ? value : base.Id; } } #region Overrides of IdentityUser<Guid,MemberLogin,MemberRole,MemberClaim> public override string PasswordHash { get { return base.PasswordHash; } set { base.PasswordHash = !string.IsNullOrWhiteSpace(value) ? value : base.PasswordHash ; } } #endregion #endregion public Guid Identity { get { return Id; } set { if (value != Guid.Empty) { Id = value; } } } public string Moniker { get; set; } [MaxLength(256)] public string FirstName { get; set; } [MaxLength(256)] public string LastName { get; set; } [MaxLength(256)] public string Middle { get; set; } public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<MemberEntity, Guid> 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; } #region Implementation of IAuditable public DateTime DateTimeCreated { get; set; } public DateTime? DateTimeModified { get; set; } public DateTime? DateTimeDeleted { get; set; } public DateTime? DateTimeArchived { get; set; } public string CreatedBy { get; set; } public string ModifiedBy { get; set; } public string DeletedBy { get; set; } public string ArchivedBy { get; set; } public bool IsDeleted { get; set; } public bool IsArchived { get; set; } #endregion } } 

Secondly, I tried the OnModelCreating method in my DbContext like this:

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { if (modelBuilder == null) throw new ArgumentNullException("modelBuilder"); modelBuilder.Entity<WebSiteEntity>() .HasKey(site => site.Identity) .ToTable("WebSite"); #region Security modelBuilder.Entity<MemberEntity>() .ToTable("Member") .HasMany(u => u.Roles) .WithRequired() .HasForeignKey(ur => ur.UserId); modelBuilder.Entity<MemberEntity>() .HasMany(u => u.Claims) .WithRequired() .HasForeignKey(uc => uc.UserId); modelBuilder.Entity<MemberEntity>() .HasMany(u => u.Logins) .WithRequired() .HasForeignKey(ul => ul.UserId); modelBuilder.Entity<MemberEntity>() .Property(u => u.Moniker) .HasMaxLength(50) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = true, IsClustered = false, Order = 2 })) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("MonikerIndex") { IsUnique = true, IsClustered = false, Order = 1 })); modelBuilder.Entity<MemberEntity>() .Property(u => u.LastName) .HasMaxLength(256) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = true, IsClustered = false, Order = 3 })) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("MonikerIndex") { IsUnique = true, IsClustered = false, Order = 2 })); ; modelBuilder.Entity<MemberEntity>() .Property(u => u.FirstName) .HasMaxLength(256) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = true, IsClustered = false, Order = 4 })) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("MonikerIndex") { IsUnique = true, IsClustered = false, Order = 3 })); ; modelBuilder.Entity<MemberEntity>() .Property(u => u.Middle) .HasMaxLength(256); modelBuilder.Entity<MemberEntity>() .Property(u => u.UserName) .IsRequired() .HasMaxLength(256) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = true, IsClustered = false, Order = 1 })) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("MonikerIndex") { IsUnique = true, IsClustered = false, Order = 4 })); modelBuilder.Entity<MemberEntity>() .Property(u => u.Email) .HasMaxLength(256); modelBuilder.Entity<MemberRole>() .HasKey(userRole => new { userRole.UserId, userRole.RoleId }) .ToTable("MemberRole"); modelBuilder.Entity<MemberLogin>() .HasKey(login => new { login.UserId, login.ProviderKey, login.LoginProvider }) .ToTable("MemberLogin"); modelBuilder.Entity<MemberClaim>() .ToTable("MemberClaim"); modelBuilder.Entity<RoleEntity>() .ToTable("Role"); modelBuilder.Entity<RoleEntity>() .Property(r => r.Name) .IsRequired() .HasMaxLength(256) .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("RoleNameIndex") { IsUnique = true })); modelBuilder.Entity<RoleEntity>() .HasMany(r => r.Users) .WithRequired() .HasForeignKey(ur => ur.RoleId); #endregion 
+1
source share

All Articles