How can I use different table in asp dot net identity rather that identityUser table, what configuration need to be done in the dbconxtext, controller and program.cs file?

Anthony Poudel 0 Reputation points
2024-02-26T10:57:54.2766667+00:00

I am beginner in Asp dot net core and I want to use make login functionality in my project but don't have an idea on how to do it, so if you can help me it would be great.

Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
683 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,506 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. SurferOnWww 2,661 Reputation points
    2024-02-27T00:53:35.46+00:00

    How can I use different table in asp dot net identity rather that identityUser table

    What do you mean? If you use the template of Visual Studio to create ASP.NET Core project with the ASP.NET Core Identity the SQL Server database will be created by default, as shown below:

    Identity

    Are you saying that you want to use your own custom database, unlike above?

    0 comments No comments

  2. Zhi Lv - MSFT 32,146 Reputation points Microsoft Vendor
    2024-03-06T03:16:43.0466667+00:00

    Hi @Anthony Poudel

    I am beginner in Asp dot net core and I want to use make login functionality in my project but don't have an idea on how to do it, so if you can help me it would be great.

    Asp.net core is an API and Razor Class Library. we can use the Scaffold Identity to generate pages related to user management features, then use the provided methods to manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

    In this scenario, you have to use the identity related table such as the AspNetUsers table, if you want to add custom data to the Identity table, you can refer to this tutorial: Add, download, and delete custom user data to Identity in an ASP.NET Core project.

    How can I use different table in asp dot net identity rather that identityUser table, what configuration need to be done in the dbconxtext, controller and program.cs file?

    If you don't want to use Asp.Net Core Identity, and want to create the User table and the related pages (for example the register and login page) by yourself, it is similar to use EF core in asp.net core, you have to do the following:

    1.Install the EF core Nuget packages (using the sql server database):

    • Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.SqlServer
    • Microsoft.EntityFrameworkCore.Design

    2.Create the Uset Entity

        public class User
        {
            public int ID { get; set; }
            public string UserName { get; set; }
            public string Password { get; set; }
        }
    

    3.Create ApplicationDBContext:

        public class ApplicationDBContext : DbContext
        {
            public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
            {
            }
            public DbSet<User> Users { get; set; } 
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            { 
                modelBuilder.Entity<User>().ToTable("UserTB");
            }
        }
    

    4.Register the ApplicationDBContext in the Program.cs file:

    // Add services to the container.
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
    builder.Services.AddDbContext<ApplicationDBContext>(options =>
        options.UseSqlServer(connectionString));
    builder.Services.AddDatabaseDeveloperPageExceptionFilter();
    

    5.Open the appsettings.json file and add a connection string as shown in the following:

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"
    }
    
    1. Execute the following commands (in the Package Manager Console) to migration and generate the database and datatable:
    add-migration generatedatabase
    update-database
    

    use SSMS check the database, the user table already generated.

    User's image

    Then, you can create the relate pages to achieve the login functionality and inject the ApplicationDBContext to the controller or razor page and use it to access data in the database.

    More detail information, you can refer to the following tutorial:

    Tutorial: Get started with EF Core in an ASP.NET MVC web app

    Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial 1 of 8


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.