Esempio n. 1
0
        public async Task <ActionResult> AuthorizationCodeAsync()
        {
            try
            {
                var config       = new GooglePeopleOAuth2Config();
                var state        = Session["OAuth2XSRFState"]?.ToString();
                var clientId     = ConfigurationManager.AppSettings["GooglePeopleOAuth2ClientId"];
                var clientSecret = ConfigurationManager.AppSettings["GooglePeopleOAuth2ClientSecret"];

                var authorizationCode = config.GetAuthorizationCode(Request, state);

                var accessTokenResult = await config.GetAccessTokenAsync(
                    clientId,
                    clientSecret,
                    "https://localhost:44348/OAuth2/Google/People/AuthorizationCode",
                    authorizationCode);

                if (!string.IsNullOrEmpty(accessTokenResult.Error))
                {
                    return(View("~/Views/GooglePeopleOAuth2/Error.cshtml", (object)accessTokenResult.Error));
                }
                else
                {
                    var data = await config.GetDataAsync("emailAddresses/value,names(displayName,familyName,givenName)");

                    if (data == null)
                    {
                        return(View("~/Views/GooglePeopleOAuth2/Error.cshtml", "Unable to get data from the server."));
                    }

                    return(View("~/Views/GooglePeopleOAuth2/Welcome.cshtml", data));
                }
            }
            catch (UserCancelledException userCancelled)
            {
                return(View("~/Views/GooglePeopleOAuth2/UserCancelled.cshtml", (object)userCancelled.Message));
            }
            catch (InvalidStateException invalidState)
            {
                return(View("~/Views/GooglePeopleOAuth2/InvalidState.cshtml", (object)invalidState.Message));
            }
            catch (Exception ex)
            {
                return(View("~/Views/GooglePeopleOAuth2/Error.cshtml", (object)ex.Message));
            }
        }
Esempio n. 2
0
        public ActionResult Index()
        {
            var googleOAuth2Config = new GooglePeopleOAuth2Config();
            var clientId           = ConfigurationManager.AppSettings["GooglePeopleOAuth2ClientId"];
            var state = "abcd";

            HttpContext.Session["OAuth2XSRFState"] = state;

            if (string.IsNullOrEmpty(clientId))
            {
                return(new HttpStatusCodeResult(500, "The application hasn't been configured correctly. It will not take any requests."));
            }

            var signInLink = googleOAuth2Config.GetSignInUrl(
                clientId,
                "https://localhost:44348/OAuth2/Google/People/AuthorizationCode",
                "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
                state);

            return(View((object)signInLink));
        }