How to get email, gender and username from Google OpenID Provider

后端 未结 2 463
無奈伤痛
無奈伤痛 2021-01-15 02:02

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



        
相关标签:
2条回答
  • 2021-01-15 02:35

    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.

    0 讨论(0)
  • 2021-01-15 02:42

    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.

    0 讨论(0)
提交回复
热议问题