Search code examples
oauth-2.0blazorgoogle-oauthasp.net-identityblazor-server-side

Get email, phone, etc from OAuth2 login


I am working on adding the ability to authenticate in my app using Google OAuth2. My app is a Blazor Interactive Server using the Identity Library (not Blazor Identity). I don't have this working yet. But once I do, how from that identity can I get that users:

  • Some GUID to uniquely identify them (emails can change)
  • Their email.
  • Their cell phone number
  • Their home address

Solution

  • To implement Blazor Server OIDC, you could reference https://stackoverflow.com/a/64857883/20240963. The claims you want need following scopes

    options.Scope.Add("email");
    options.Scope.Add("profile");
    options.Scope.Add("https://www.googleapis.com/auth/user.phonenumbers.read");
    options.Scope.Add("https://www.googleapis.com/auth/user.addresses.read");
    

    Then User will be prompt with if they are willing to share phone number and address to login.

    For the identifer you don't need special scope which is generally the 1st claim.

    <AuthorizeView>
        <Authorized>
            @context.User.Claims.FirstOrDefault().Value
        </Authorized>
    </AuthorizeView>
    

    Let's say as phone scams are becoming increasingly rampant these days, people doesn't want to leak phone number and Home Address when login to a website.