Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What binds ApplicationUser to the aspnetusers table in Database First?

I have a database-first EDMX model in a separate library (e.g. Common.Feedback.Data), that includes the AspNetUser table and its related Identity Framework tables (pulled from another existing, working, database/application).

I have updated the ApplicationDbContext connection string to point to the new model and new database connection:

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace Feedback.MvcApplication.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.

    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 class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("FeedbackEntities", throwIfV1Schema: false)
        {
        }

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

The connection string in the web.config includes the full path to the assembly and that reference is fine:

<add name="FeedbackEntities" 
     connectionString="metadata=res://Common.Feedback.Data/FeedbackModel.csdl|res://Common.Feedback.Data/FeedbackModel.ssdl|res://Common.Feedback.Data/FeedbackModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser@mydatabase;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

At runtime, any access to login results in the following error:

"The entity type ApplicationUser is not part of the model for the current context"

All links I have tried, relating to that error, usually involve ways of updating the migration.

I cannot enable migrations as it is an EDMX first setup, but I assume there is a binding between the ApplicationUser object and the aspnetusers table "somewhere" behind the scenes.

Does anyone here have a clear understanding of how the ApplicationUser class is mapped to/from the aspnetusers table at runtime and explain how to get my code to work with the database?

Repro steps

  • Create a new MVC Web Application using VS 2013
  • Update all NuGet packages to latest
  • Take an existing SQL database with the Identity Framework tables and copy it to a new table (strip out any unrelated tables).
  • Add new tables for the project
  • Create a class library to hold the data models (e.g. Common.Feedback.Data)
  • Add an Edmx data model, to the library, based on the database created previously
  • Change the connection string to fully qualify the assembly (not res://*/)
  • Change the connection string name in IdentityModel.cs to match the connection string name in the config.
  • Copy the connection string from the library's app.config to the web project's web.config
  • Try login and you will hit the error mentioned

Update:

Based on a stray post, I changed my connection string to match the normal SQL connection the Identity Framework is configured to use by default (and using a Sql client):

<add name="FeedbackSql" 
     connectionString="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser@mydatabase;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework" 
     providerName="System.Data.SqlClient" />

and changed to setup to use this new connection:

   public ApplicationDbContext()
        : base("FeedbackSql", throwIfV1Schema: false)
    {
    }

Bizarrely the error changes to:

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

I think the initial change to a SqlClient provider is correct, so this new error may related to using an Azure database over that connection. I am open to suggestions on what to try next.

Updated web.config based on suggestion by @rism and this link (but the GZip error persists):

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <!--<parameter value="mssqllocaldb" />-->
        <parameter value="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=myuserid;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

Again based on tips from @rism I also tried this version (but the GZip error persists):

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

New update:

I created a brand new Web application with the standard user-security option. I also created an empty database in Azure.

I did nothing but change the default connection string to this:

  <connectionStrings>
    <add name="DefaultConnection" 
         connectionString="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

and the default connection factory to this:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

On attempting to login I get the following error:

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

Source Error:

Line 153: { Line 154: var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; Line 155: var result = await UserManager.CreateAsync(user, model.Password); Line 156: if (result.Succeeded) Line 157: {

like image 685
Gone Coding Avatar asked Jan 27 '15 22:01

Gone Coding


People also ask

How do I add a field to the default AspNetUser table?

To add additional columns to ASP.Net Core Identity, we need to create a model inheriting the IdentityUser properties. This method will allow us to specify an additional field for IdentityUser. To do so, make a model class name ApplicationUser.

What is ASP net Core Identity used for?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

What is identity in Entity Framework?

ASP.NET Core Identity provides a framework for managing and storing user accounts in ASP.NET Core apps. Identity is added to your project when Individual User Accounts is selected as the authentication mechanism. By default, Identity makes use of an Entity Framework (EF) Core data model.

How can I change the table names when using ASP NET identity?

You can do this easily by modifying the IdentityModel. cs as per the below: Override OnModelCreating in your DbContext then add the following, this will change AspNetUser table to "Users" you can also change the field names the default Id column will become User_Id.


1 Answers

The original question was about how does OWIN map the ApplicationUser class to the AspNetUsers table.

I decompiled the Identity Framework dlls with DotPeek and found that the table name "AspNetUsers" is simply hard-wired into the code of the OnModelCreating method. e.g.

 protected virtual void OnModelCreating(DbModelBuilder modelBuilder)
    {
      if (modelBuilder == null)
        throw new ArgumentNullException("modelBuilder");
        EntityTypeConfiguration<TUser> typeConfiguration1 = ((EntityTypeConfiguration<TUser>) modelBuilder.Entity<TUser>())
        .ToTable("AspNetUsers");

That code uses reflection to generate a list of field names from the properties in ApplicationUser. It checks for the existence of all those field names using a simple SQL query (of the system table containing all field names) and confirms they are all present.

The rest is simple mapping of names/values using constructed SQL queries.

As for the other part of the problem, the database connection, I have created a new question as it is a) unrelated to the title of this one and b) driving me crazy! The new question is Why am I getting "The magic number in GZip header is not correct." error using OWIN auth against Azure SQL

like image 186
Gone Coding Avatar answered Oct 11 '22 13:10

Gone Coding