3

I'm pretty new to ASP.NET Core 5 Identity, and I have several questions (that I will break in several posts here on StackOverflow) but please, while answering, consider that my knowledge about this topic is limited as I have little to no experience with this system.

I have an intranet application (Blazor server side, lets call it Application A) where its users can create guest users that will use an android app (through a different, public, WEB API application, lets call this Application B) to access their data. The users of Application B can NOT register by themselves, their accounts can ONLY be set up by the users of Application A.

I want to keep the users of the android app (Application B) on a different database (I don't want to mix the two since Application A holds sensitive data and is an intranet application while Application B is publicly accessible from the Internet).

Both applications (A and B) will use ASP.NET Identity to authenticate their respective users but each one has its own independent database (so each one has its own independent Identity tables), of course Application A can access the database of Application B but NOT the other way around (Application B can't see Application A's database).

In short: I want to create users from Application A in the database of Application B. How can I do that?

Is there any example that would help me accomplish something like this? As I said, I have very little experience with ASP.NET Identity so any example would be very helpful.

Thank you

P.S.: I hope I have been sufficiently clear, English is not my native language.

EDIT: I'm going to add some code to show what I have now.

This is how the Identity framework is currently set-up in my Startup.cs file. As you can see there is an ApplicationDbContext that points to the Blazor's database (Application A) and the Identity uses the same ApplicationDbContext to read and write to the database.

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

How do I add a SECOND Identity Framework to point to a different DbContext to read and write from/to the database of Application B (the WEB-API used by my Android's app)?

Right now if I wanted to create a new user in Application A's database programmatically I would do this from my Blazor (Application A) page:

@using Microsoft.AspNetCore.Identity;
@inject UserManager<IdentityUser> userManager;

<button class="btn btn-danger" @onclick="AddUser">Add User</button>

@code {
    private async Task AddUser()
    {
        var user = new IdentityUser() { Email = "john@test.com", UserName = "john@test.com" };
        await userManager.CreateAsync(user);
        await userManager.AddPasswordAsync(user, "TestPassword_123");
        await userManager.ConfirmEmailAsync(user, await userManager.GenerateEmailConfirmationTokenAsync(user));
    }
}

How would I do the same but inside Application B's database (but still from Application A)?

Luca
  • 31
  • 1
  • 4
  • 1
    You may want to start with MS Identity tutorial. Once you master that one, it will be easier to show code and ask "but I also want to do X". It seems that what you are asking is quite straightforward - you create two `DataContext`s in Application A, and use one of them in application B (word *website* is very confusing in the context of your question). But if you are asking something different, showing the code is best way to clarify – Felix Jul 27 '21 at 06:47
  • You can implement your own `IUserStore` (inherit from [`UserStoreBase`](https://github.com/dotnet/aspnetcore/blob/746b9f82fb5c026ce3ce1aed9b2883078ca9ebe6/src/Identity/Extensions.Stores/src/UserStoreBase.cs)) and fetch the user information from that _different_ source (dbcontext, api, etc.) – abdusco Jul 27 '21 at 07:13
  • @Felix Thank you for your answer. I have edited my question to change the word "website" to "application" in the hope of making the question clearer. I have also followed your suggestion to add some code to show what I have right now and what I hope to accomplish. – Luca Jul 27 '21 at 19:02
  • @abdusco Unfortunately I believe that right now I don't know Identity well enough to follow your suggestion. Wouldn't changing the UserStore also affect the database in Application A? I still want to be able to access/read/delete/etc users in the database of Application A (a Blazor project), but i **also** want to do the same in the database of Application B (a WEB-API project) from user interface of Application A. Sorry if the question is stupid but I am having a hard time understanding what you mean right now. – Luca Jul 27 '21 at 19:07
  • https://stackoverflow.com/questions/47433269/multiple-identities-in-asp-net-core-2-0/47434573#47434573 – abdusco Jul 27 '21 at 19:18
  • 1
    Problem with [that answer](https://stackoverflow.com/questions/47433269/multiple-identities-in-asp-net-core-2-0/47434573#47434573), though, is that it needs a separate user type for the second identity (to register a separate set of management services). You can just an empty subclass for that: `class AppUser: IdentityUser {}` than map that entity to the same table as `IdentityUser` – abdusco Jul 27 '21 at 19:26

0 Answers0