Azure AD authentication combined with individual user accounts authentication for WebAssembly Asp.net .NET 5 hosted

Javad 1 Reputation point
2021-09-29T16:33:50.863+00:00

I am creating an app where some users can create an individual account and sign in with that. Also, I need users can sign in if they have Azure AD. I used Identity Server 4 for the individual accounts and it works correctly.

for Azure Ad, I found some answers on Stackoverflow and based on it I implemented my code as follows:

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddDefaultIdentity<ApplicationUser>(options => 
            options.SignIn.RequireConfirmedAccount = true)
                            .AddRoles<ApplicationRole>()
                            .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddIdentityServer()
                            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
                            .AddProfileService<IdentityProfileService>();

            services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            }).AddOpenIdConnect("AAD", "Azure Active Directory", options =>
                            {
                                options.ClientSecret = "<Secrete>";
                                options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
                                options.ClientId = "<ClientId>";
                                options.Authority = "https://login.microsoftonline.com/<tenantId>/";
                                options.CallbackPath = "/authentication/login-callback";
                                options.SaveTokens = true;
                                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

                            }).AddCookie()
            .AddIdentityServerJwt();

I also created the callback URI in Azure as a web platform.

when I test that in the Postman, it works correctly and it gets the token. However, in my app, it redirect that to https://localhost:5001/identity/account/externallogin?returnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id ... and it shows "Sorry, there's nothing at this address."

I think this is because I am using WebAssembly with ASP.Net .NET 5 hosted.

Could someone help me with this or show me a sample?

Also, for authorization part , I also need manage users and roles inside the database and if there is not the user there, create a user in that user table.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,382 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,424 questions
{count} votes