question

AlexanderDundua-8666 avatar image
0 Votes"
AlexanderDundua-8666 asked FriQenstein-7290 commented

ASP.NET Core 6 Identity - Scaffolding Identity problem

I am following the tutorial on docs.microsoft.com (Authentication, ASP.NET Core 6.0).

I create ASP.NET Core Web App with individual user accounts and update a database. So far everything works. Then I scaffold the identity.

Then I Scaffold Register, Login, LogOut, and RegisterConfirmation according to the tutorial. Here I select "Login", "Logout" etc., select ApplicationDbContext as Data Context Class and click add.

The problem is that in program.cs it generates additional connection string (so there are two of them), as well as dublicates of builder.Services.AddDbContext... and builder.Services.AddDefaultIdentity...

Everything works fine if I delete (comment out) this newly generated lines but I do not know what am I doing wrong.


dotnet-aspnet-core-generaldotnet-aspnet-core-razordotnet-aspnet-core-auth
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Then I Scaffold Register, Login, LogOut, and RegisterConfirmation according to the tutorial. Here I select "Login", "Logout" etc., select ApplicationDbContext as Data Context Class and click add.

Can you tell how you did it? What are the settings of Data Context Class and User Class?

In my environment the builder.Services.AddDbContext... and builder.Services.AddDefaultIdentity... in the Program.cs generated by the Visual Studio 2022 are as follows:

 var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
    
 builder.Services.AddDbContext<ApplicationDbContext>(options =>
     options.UseSqlServer(connectionString));
    
 builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
     .AddEntityFrameworkStores<ApplicationDbContext>();


Are they the same in your environment? The duplications are also the same?

0 Votes 0 ·

This is still an issue as of July 22, 2022. In fact, it has gotten worse.
When I attempt to Scaffold the Register page, as OP mentioned, it now creates an additional Data directory in the Areas->Identity directories.

It never did this in the past, especially considering I already have an ApplicationDbContext.cs and an existing Data directory in the standard structure of the solution... as generated by VS2022.

Simply deleting this directory also does not fix the issue, because it branches out to other errors that you must hunt down, which takes more time.

Having to track down these errors wastes time and creates frustration. This completely negates the basic principle of not wasting time while coding.

I am attaching snippets of BEFORE and AFTER the scaffolding to show what it was and what it creates.

Regards.
223832-capture1.jpg223863-capture2.jpg



0 Votes 0 ·
capture1.jpg (10.9 KiB)
capture2.jpg (17.3 KiB)

An update, because for some reason this forum will not let me EDIT a post....

When I scaffolded the Register page, and got the errors mentioned above, it not only created those duplicates, but it also decided to register everything based on IdentityUser instead of Employee, which is what I have already set everything to use PRIOR to scaffolding the Register page.

Why is it deciding to completely ignore my existing configuration?
i.e. in the Register.cshtml.cs file, everything is registered as IdentityUser instead of what I changed it to at the very beginning of my project creation, Employee.
The ENTIRE file is permeated with IdentityUser references instead of what is actually being used and already setup in the Program.cs file.

0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @AlexanderDundua-8666,

The problem is that in program.cs it generates additional connection string (so there are two of them), as well as dublicates of builder.Services.AddDbContext... and builder.Services.AddDefaultIdentity...

Everything works fine if I delete (comment out) this newly generated lines but I do not know what am I doing wrong.

I could reproduce the problem on Asp.net 6 application and use VS 2022. This error appeared recently and only on Asp.net 6, it does not happen on the Asp.Net 5 and the previous version. And I found this issue has been feedback to the Asp.net development team, you can check it and wait the response.

As a temporary solution, as you said you can delete or comment out the duplicate lines. Or you can create an Asp.net 5 or previous version application.


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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.