Asp.Net 6

Aaron Nguyen 1 Reputation point
2022-10-20T18:48:30.163+00:00

I am trying to add google authentication to my .net new mvc application however whats confusing me is the documentation steps to properly integrate it in to my app and when looking at yt videos its all related to Visual studio not visual studio code .

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 61,096 Reputation points
    2022-10-20T19:44:57.16+00:00

    The docs are pretty clear and they don't mention Visual Studio at all. The actual code involved has nothing to do with VS vs VS Code as it ultimately compiles the same. Refer to the linked docs and walk through the process and you should be fine.

    If you have specific questions then please ask those and provide us the relevant code.

    0 comments No comments

  2. Anonymous
    2022-10-21T09:47:15.47+00:00

    Hi @Aaron Nguyen ,

    To implement Google Authentication, first we need to get the Google client ID and secret. You can refer to the Google Authentication document and Create the Google OAuth 2.0 Client ID and secret.

    Then, depending on whether you are using Asp.net core Identity or not, you can refer the following steps to implement Google Authentication in an MVC application.

    If using Asp.net core Identity, since the ASP.NET Core Identity has a built-in support for authentication service that works on OAuth like Google, Facebook, Microsoft, LinkedIn, Twitter, etc. There is no need to add the External login button by ourselves. In this scenario, you can do the following steps:

    1. Install the Microsoft.AspNetCore.Authentication.Google package via Nuget.
    2. Configure the Google Authentication Pipeline.
      Open the appsettings.json file and add the Google configuration (in my sample, I'm using appsettings.json file to store the google clientid and secret, you can also refer to the official document to store the values with Secret Manager). Like this:
      252749-image.png
      Open the Program.cs file, add the yellow part of code:
      252820-image.png
      Then, you can find the Google Login button in the Identity Login page (you can use Scaffold to add the Identity pages, refer to Scaffold Identity in ASP.NET Core projects) and login with Google account. 252925-1.gif

    If you want to implement Google Authentication without Asp.net Identity, you have to add the Google Login button by yourself. Refer to the following sample:

    1. Create a MVC application without select the "Individual Accounts" Authentication Type.
    2. Install the Microsoft.AspNetCore.Authentication.Google package via Nuget.
    3. Configure the Google Authentication Pipeline.
      Still use appsettings.json file to store the Google ClientId and secret.
      Add the yellow part of code in the Program.cs file,
      252962-image.png
    4. Create an Account controller to add the Google Login and Login Response endpoints:
      252981-image.png
    5. Add the Google Login button in the _Layout.cshtml page:
      252972-image.png
      You can view the relates code from here: 252944-sourcecode.txt
      After that, the result as below:
      252935-2.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

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.