How to implement Google Authetication in Blazor server app without Core Identity

biprism 51 Reputation points
2022-04-29T01:37:58.587+00:00

Hello

I would like to use custom authentication without using out-of-the-box Core Identity in my Blazor Server App with Oauth authentication.

I started the app using the template without using individual user account because we have our own registration and login page.

But the real issue is we would like to implement OAuth with external provider such as google.

Could someone point to right direction.?

I got this link from MS Docs Facebook, Google, and external provider authentication without ASP.NET Core Identity | Microsoft Doc...

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,460 questions
0 comments No comments
{count} vote

Accepted answer
  1. Zhi Lv - MSFT 32,056 Reputation points Microsoft Vendor
    2022-04-29T07:59:23.14+00:00

    Hi @biprism ,

    Here is a simple sample about implement Google Authentication in Blazor server application without using Asp.net core Identity, you can check it:

    1. Create a new Asp.net core Blazor Server application, without select the "Individual Accounts" type.
    2. Install the Microsoft.AspNetCore.Authentication.Google package via Nuget.
    3. Refer to the Google Authentication document and Create the Google OAuth 2.0 Client ID and secret.
    4. Configure the Authentication Pipeline.
      Open the appsettings.json file and add the Google configuration. Like this: 197607-image.png Open the program.cs file (this is a Asp.net 6 application, if you are using the lower version, you can add the configuration in the startup.cs file), add the yellow part of code: 197634-image.png
    5. Create Login page, Logout page and LoginControl component: login part You can view the page source from here: 197626-loginpage.txt 197627-logoutpage.txt 197692-logincontrol.txt Then, add the LoginControl component in the MainLayout.razor component: 197628-image.png

    Finally, running the application, the result like this:

    197608-1.gif


    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 people found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Jason Mock 5 Reputation points
    2023-12-06T18:57:10.75+00:00

    Threw the following together as a quick exercise based on this question that's updated to .NET 8 with improvements to the login/logout pages and using the AuthenticationStateProvider:

    https://github.com/mockjv/blazor-google-auth

    1 person found this answer helpful.

  2. Sojan Chandy 5 Reputation points
    2024-01-24T08:35:09.0733333+00:00

    error.pngif you make a blazor server application in .net8 it won't work. but if you create a blazor server application in .net 7 and change the project to .net 8 it will work. Do you know how to make it work for the .net 8 application made from scratch? User's image

    1 person found this answer helpful.

  3. biprism 51 Reputation points
    2022-05-05T01:13:07.617+00:00

    Thanks for the answer. . This is a follow up question. This solution uses MVC Razor pages for LOgin and Logout. that inherits from PageModel. I do Blazor Server app what will be equivalent if I use blazor server pages that does not have OnGet and call back method.?


  4. Rob McMahon 0 Reputation points
    2023-09-14T18:14:33.9433333+00:00

    Thanks for providing the solution, it helped a lot with getting my Blazor project built. I did run into a problem in that it worked great on my local host but failed with a null httpContext when it was deployed to the server.

    After some searching around I found a way around this. In LoginControl.razor remove the httpContext and htppContextAccessor and replace with AuthenticationStateProvider.

    @inject AuthenticationStateProvider AuthenticationStateProvider
    
    ...
            
    var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
    User = authState.User;
    

    Credit to Richard Baker for this, https://learn.microsoft.com/en-us/answers/questions/1157277/blazor-server-ihttpcontextaccessor-returning-null

    0 comments No comments