1

I am looking to setup a process to validate a user from Google via OAuth, here is the expected behavior I want to create.

The user goes to a url that I provide, my application goes back to Google to see if they are logged in and authenticates them if they are not logged in, then have a Google "Log Me In" prompt appear and user completes the authentication.

After the user is authenticated, I want to collect their basic info (Email, First and Last Name) etc.

Once I have that, I will be doing other stuff, but I am good there.

I have read around, but I'm having a hard time getting my head around the whole OAuth Authentication process and how it plugs into Google. Can someone point me in the direction of what I am trying to get?

I have created a product in Google APIs and obtained a Client ID and secret, so I think I am ready to spin up a C# App.

4

1 回答 1

1

您需要的一切都包含在这个包中: http: //dotnetopenauth.net/

查看他们的示例,它将向您展示如何请求部分用户信息,以及如何阅读它们。

[编辑]

      const string CallbackParameter = "callback";
      const string Endpoint = "https://www.google.com/accounts/o8/id";

      using (var openid = new OpenIdRelyingParty())
      {
            var callbackUrl = new Uri(string.Format("{0}?{1}=true", _context.RequestUri.AbsoluteUri, CallbackParameter));
            var authRequest = openid.CreateRequest(Endpoint, new Realm(string.Format("{0}://{1}", _context.RequestUri.Scheme, _context.RequestUri.Authority)), callbackUrl);

            // Tell Google what we want
            var fetch = new FetchRequest();
            fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
            authRequest.AddExtension(fetch);

            var response = authRequest.RedirectingResponse;
            var location = response.Headers["Location"];

            var fetch = response.GetExtension<FetchResponse>();
            if (fetch != null)
            {
                Username = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                Email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                FirstName = fetch.GetAttributeValue(WellKnownAttributes.Name.First);
                LastName = fetch.GetAttributeValue(WellKnownAttributes.Name.Last);
            }
      }
于 2013-09-25T15:45:52.263 回答