Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom table in ASP.NET IDENTITY?

I'm using ASP.NET Identity on my web form application. Below is my current identity tables:

Current Identity tables

 - Role
 - User
 - UserClaim
 - UserLogin
 - UserRole

I need to add a new table to store additional information.

New Table: UserLogs

Fields inside the new table:

  • UserLogID (PK)
  • UserID (FK)
  • IPAddress
  • LoginDate

How can I add this custom table? I know how to add custom fields inside existing table but I don't know how to achieve this.

I appreciate your efforts in reaching a solution for my problem.

like image 232
Kevin Maxwell Avatar asked Aug 11 '15 12:08

Kevin Maxwell


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 IdentityRole?

It contains default properties such as username, email, password e.t.c. This class can be inherited and more properties provided. IdentityRole is the ASP.NET Core MVC class that contains information about user roles (which are usage domains) of the IdentityUsers defined in your application.

How does core identity work in asp net?

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.

What is IdentityDbContext?

IdentityDbContext provides all of the EF code-first mapping and DbSet properties needed to manage the identity tables in SQL Server.


2 Answers

public class ApplicationUser : IdentityUser
{

    public virtual ICollection<UserLog> UserLogs { get; set; }

}

public class UserLog
{
    [Key]
    public Guid UserLogID { get; set; }

    public string IPAD { get; set; }
    public DateTime LoginDate { get; set; }
    public string UserId { get; set; }

    [ForeignKey("UserId")]
    public virtual ApplicationUser User { get; set; }
}

public System.Data.Entity.DbSet<UserLog> UserLog { get; set; }
like image 171
Kevin Maxwell Avatar answered Nov 08 '22 12:11

Kevin Maxwell


first of all I'd suggest you to understand a bit about Code-First Entity Framework, because that's how those tables about users is created when you first create New MVC5 Applictation.

Here you can find how it is implemented if you want to use code-first database creating through the EntityFramework.

After you understand some core stuff about it, you can go to your project on Models folder, straight to> AccountModel.cs

There you have a Class which extends DbContext, which inside contains the constructor which gives access to the connectionstring on which database is about to be used.

Short important stuff if u are lazy on reading code first too much:

DbSet means that the model inside dbset, is about to be created into a table. Variables inside class, are columns that are about to be created. But if you want foreign keys and stuff, you definitely have to read code-first kind of.

Good Luck!

like image 31
Festim Cahani Avatar answered Nov 08 '22 12:11

Festim Cahani