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

后端 未结 2 462
無奈伤痛
無奈伤痛 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();
                    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.

提交回复
热议问题