Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

.net 5 - How to add Identity Services in ASP.NET Core 5

I'm trying to add Identity Services.

And I'm trying to change the number of columns in the user table. I don't know how to correct it.

  • Version - .NET 5
  • Identity nuget version - 5.0.1

When I change

// IT DOESN'T WORK
services.AddIdentityCore<User>() 
        .AddEntityFrameworkStores<ApplicationContext>();

to

 // this works
services.AddIdentityCore<IdentityUser>()
        .AddEntityFrameworkStores<ApplicationContext>();

Code:

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
    services.AddIdentityCore<User>().AddEntityFrameworkStores<ApplicationContext>();
    
    services.AddRazorPages();
}
    
public class User : IdentityUser
{
    public int Year { get; set; }
}    
question from:https://stackoverflow.com/questions/65601116/how-to-add-identity-services-in-asp-net-core-5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try to change like below:

services.AddDefaultIdentity<User>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

Be sure your DbContext like below:

public class ApplicationDbContext : IdentityDbContext<User>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

Be sure update _LoginPartial.cshtml and replace IdentityUser with User:

@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager

Finally,you could scarffold the Identity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...