Trying to add MicrosoftAccounts external authentication

EJ 0 Reputation points
2024-03-12T08:55:38.23+00:00

User's image

I built a test app as I was trying to check how Microsoft Authentication works. I created blazor ssr app then added the code above. But seems like it can't find the extension. I tried with the recomended WebApp but sill having the same issue.

Also, i tried adding the nuget dotnet add package Microsoft.AspNetCore.Authentication.MicrosoftAccount --version 8.0.2 then i started getting the error message "Can't find the namespace App..." which is located on the program.cs. I've removed the package then the error message is gone. I tried to add the package with the version 8.0.0 to WebApp and it worked. But not to the blazor ssr.

I was trying to integrate google authentication to the newly released blazor SSR.

Microsoft Authenticator
Microsoft Authenticator
A Microsoft app for iOS and Android devices that enables authentication with two-factor verification, phone sign-in, and code generation.
6,150 questions
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,500 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tiny Wang-MSFT 2,321 Reputation points Microsoft Vendor
    2024-03-12T12:38:58.4033333+00:00

    Hi Ej, according to your description, I'm afraid you already read the official document for adding Microsoft account as the external authentication provider. Here, I had a test in my side by creating a new blazor web app server side rendered, and choosing Individual Accounts as the Authentication type. Then I add code snippet .AddMicrosoftAccount following the document to Program.cs which looks like below. Then I can get the result like the screenshot. Please note, the external authentication requires to use alone with the asp.net core default identity.

    builder.Services.AddAuthentication(options =>
        {
            options.DefaultScheme = IdentityConstants.ApplicationScheme;
            options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
        })
        .AddIdentityCookies();
    builder.Services.AddAuthentication()
       .AddMicrosoftAccount(microsoftOptions =>
       {
           microsoftOptions.ClientId = "client_id";
           microsoftOptions.ClientSecret = "client_secret";
       });
    

    Installing the Nuget package you mentioned can solve the issue, but didn't produce "Can't find the namespace App..." error message. I create the project like screenshot below. When it worked well, I can see external sign in entrance over there.

    User's image

    User's image

    I also tried to add the code snippet in a project which doesn't have individual account authentication type, but installing the package can solve the no reference compilation error as well. So that I'm afraid there's some other differences between your code and mine. If you don't care why you had that error message after installing the nuget package, I suggest you creating a new project for test if possible.

    You also mentioned that you want to add google authentication into blazor web app, if you want to do like what I shared above, you might follow the document here to integrate it. If you just want to have an app which only support Google account signing in, I'm afraid you could go to Google Authentication support team for help.