0

I have developed my first application using ASP.NET Core MVC 5.0.

There was a database already existing but I implemented full database in new program and now the issue was ASP.NET Core identity. So for identity I scaffolded as per instructions and finally implemented default identity authentication using email and pass.

services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

In startup.cs and after that I have a model with existing field i.e. cardno, phone and want to authenticate them on this. In PHP it was really very easy using $_SESSION variable. But now in here I want this to happen in ASP.NET any help will be appreciated.

services.AddIdentity<CnicUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();

This code I added for my custom authentication I got this error

Application startup exception System.InvalidOperationException: Scheme already exists: Identity.Application at Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(String name, Action`1 configureBuilder)

Dale K
  • 25,246
  • 15
  • 42
  • 71

1 Answers1

0

You are trying to add identity service twice in your Startup.cs which is incorrect. So the quick solution is you have to remove either of one which will remove the exception you are having with.

So remove services.AddDefaultIdentity which you have added earlier so it will work accordingly.

Note: Deleting either of the addDefaultIdentities will work. You just can't have it in both locations in your case its creating the issue.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Farid I actually want to keep both the default identity procedure as well as want to authenticate other type of users VIA a model fields i.e cardno, phone – Malik Hamza Younas Dec 20 '21 at 06:36
  • If you want to validate user with `cardno, phone` additional feild you can use model validation but if you want to add two kind of identity together I doubt you can do it. By the way what `CnicUser` contains? – Md Farid Uddin Kiron Dec 20 '21 at 07:08
  • Still if you want to use multiple identity then you have to use `base class` then implement that base class on your new identity model. and then add that reference on `startup.cs` you can refer to this [example](https://stackoverflow.com/questions/64370175/how-to-add-multiple-identity-and-multiple-role-in-asp-net-core) – Md Farid Uddin Kiron Dec 20 '21 at 07:21
  • Farid CnicUser contains only inheritance from IdentityUser and yes i want to implement multiple identity but like default identity and other one is with two fields of tblemployee model. – Malik Hamza Younas Dec 21 '21 at 04:34
  • Well, in that case you have to use [`identity extesion`](https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Extensions.Core/src/IdentityServiceCollectionExtensions.cs). Please have a look on [this thread as well](https://stackoverflow.com/questions/47433269/multiple-identities-in-asp-net-core-2-0). Hope that would guide you accordingly – Md Farid Uddin Kiron Dec 21 '21 at 07:32
  • farid Really really thankful for your help but still it's not working in .net core 5.0 – Malik Hamza Younas Dec 22 '21 at 04:23
  • Are you getting any error? – Md Farid Uddin Kiron Dec 22 '21 at 04:24
  • yes actually let me show https://ctxt.io/2/AABgKhuREg – Malik Hamza Younas Dec 22 '21 at 04:27
  • Farid I used services.AddIdentityCore().AddEntityFrameworkStores(); Then it asks me to extend from identity user but when i extend it gives me error that identity user columns are invalid but i don't want to add identityuser columns to my table – Malik Hamza Younas Dec 22 '21 at 04:35
  • I have checked your error did you tried adding `identityuser`? additionally you could also follow the reference I referred to you. – Md Farid Uddin Kiron Dec 22 '21 at 07:35
  • Farid I tried using IdentityUserCore because identityuser after defaultidentity produces error – Malik Hamza Younas Dec 23 '21 at 06:08