How to get email, gender and username from Google

2019-02-19 22:31发布

问题:

I Have made Login system using open ID Using the following code

<rp:OpenIdLogin runat="server" Identifier="https://www.google.com/accounts/o8/id"   Visible="true" 
        ExampleUrl="" LabelText=" " RegisterText="Register"    ExamplePrefix=" " ID="OpenIdLogin1"  
        OnLoggedIn="OpenIdTextBox1_LoggedIn" RequestEmail="Require" RequestPostalCode="Request"></rp:OpenIdLogin>                     

It Takes the user to Google for authentication i only want to sotre the user Information like email ID and his Fullname and Sex to My DB

I have written the following code to retirve email from google bu nothing is retuernd

Imports System
Imports DotNetOpenAuth.OpenId.Extensions.AttributeExchange
Partial Class Food
Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If (Session("FetchResponse") Is Nothing) Then
            Return
        End If
        Dim fetchResponse As FetchResponse = CType(Session("FetchResponse"), FetchResponse)
        Email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email)
    End Sub

    Public Property Email() As String
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property
End Class

Update

I have used loginame to display the authenticated username but it displays like this https://www.google.com/accounts/o8/id?id=AItOawmQoYAeHRxYW0ZOcQ5VODMPWJQgPOAYkTs

how can i display actual email or username

回答1:

See duplicate question How to get email from Google OpenID Provider (in VB) for the answer, with the addendum that:

You cannot get the Google user's username or gender. You can get their email address and I think that's about it. (Maybe their full name as well). This is up to individual OpenID Providers and Google, like other large ones, has elected to provide minimal data on the user.



回答2:

Are you using DotNetOpenAuth? Here is how I've done it

Edit: I added the code I have for OpenID Login. Basically you make a request to the OpenID provider and get a response from your request. If the request was successful you should have some information about your user but remember that different OpenID providers give different information regarding your users, some parameters might not be there even if you requested for them. My code is from an ASP.NET MVC application

public ActionResult OpenIDLogin(string loginIdentifier)
{
    var openid = new OpenIdRelyingParty();
    var response = openid.GetResponse();

    // If there is a response from the specified OpenID identifier we parse it and check it's status
    if (response != null)
    {
        //check the response status
        switch (response.Status)
        {
            case AuthenticationStatus.Authenticated:

                var extensions = response.GetExtension<ClaimsResponse>();
                var user = new User
                        {
                            extensions.FullName, 
                            extensions.Nickname, 
                            extensions.Email,
                            response.ClaimedIdentifier
                        };

                return View();

            case AuthenticationStatus.Canceled:
                // TODO

            case AuthenticationStatus.Failed:
                // TODO
        }
    }
    else
    {
        // If there isn't a response then we need to create the request and add desired extensions
        var request = openid.CreateRequest(loginIdentifier);

        request.AddExtension
            (
                new ClaimsRequest()
                {
                    FullName = DemandLevel.Require,
                    Nickname = DemandLevel.Require,
                    Email = DemandLevel.Require,
                    Gender = DemandLevel.Require
                }
            );

        return request.RedirectingResponse.AsActionResult();
    }

    return RedirectToAction("Index", "Home");
}

The method gets called twice; once when the request is first made and it falls in the "else" statement, and a second time when the response comes back and enters the "if" statement, creating the User. Hope this helps, and sorry that it's in C# but I don't know VB that well as to write it.