コード例 #1
0
        /// <summary>
        /// The route handler for the request which connects the PhotoHunt
        /// user to Google+.
        /// </summary>
        /// <param name="context">The request/response context.</param>
        public override void ProcessRequest(HttpContext context)
        {
            User          user          = GetUser(context);
            ConnectHelper connectHelper = new ConnectHelper();

            if (user == null)
            {
                if (context.Request.Cookies[Properties.Resources.MOBILE_SESSION_COOKIEID] != null)
                {
                    context.Request.Cookies.Remove(Properties.Resources.MOBILE_SESSION_COOKIEID);
                }

                // Get the authorization code from the request POST body.
                StreamReader sr = new StreamReader(
                    context.Request.InputStream);
                string input = sr.ReadToEnd();

                TokenData td = (TokenData)(new TokenData().FromJson(input));

                // Manually perform the OAuth2 flow for now.
                // TODO(class) Use the library for code exchange once
                // "postmessage" no longer throws exceptions in URI.
                if (td.code != null)
                {
                    var authObject = ManualCodeExchanger.ExchangeCode(td.code);

                    // Create an authorization state from the returned token.
                    _authState = CreateState(
                        authObject.access_token, authObject.refresh_token,
                        DateTime.UtcNow,
                        DateTime.UtcNow.AddSeconds(authObject.expires_in));
                }
                else
                {
                    // Create an authorization state from the returned token.
                    _authState = CreateState(
                        td.access_token, td.refresh_token,
                        DateTime.UtcNow,
                        DateTime.UtcNow.AddSeconds(td.expires_in));
                }

                PhotoHunt.utils.ConnectHelper.VerifyToken(_authState);

                user = connectHelper.SaveTokenForUser(_authState);
                context.Session[Properties.Resources.CURRENT_USER_SESSION_KEY] = user.ToJson();
            }

            SendResponse(context, new JsonUser(user));
        }