コード例 #1
0
        /// <summary>
        /// Either:
        /// 1. Create a user for the given ID and credential
        /// 2. Or, update the existing user with the existing credential
        ///
        /// If 2, then ask Google for the user's public profile information to store.
        /// </summary>
        /// <param name="authState">The OAuth v2 state for authorizing the user.</param>
        /// <returns>A User object that represents the created user.</returns>
        public User SaveTokenForUser(IAuthorizationState authState)
        {
            // Set the authorization state in the superclass for the authorization call.
            _authState = authState;

            var provider = new WebServerClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = CLIENT_ID;
            provider.ClientSecret = CLIENT_SECRET;
            var authenticator =
                new OAuth2Authenticator<WebServerClient>(
                    provider,
                    GetAuthorization)
                {
                    NoCaching = true
                };

            ps = new PlusService(new BaseClientService.Initializer()
            {
                Authenticator = authenticator
            });

            Person me = ps.People.Get("me").Fetch();

            // Load the user model from the DB if the user already exists.
            bool userExists = false;
            User user = new User();
            PhotohuntContext db = new PhotohuntContext();
            User existing = db.Users.FirstOrDefault(u => u.googleUserId.Equals(me.Id));
            if (existing != null)
            {
                user = existing;
                userExists = true;
            }

            if (!userExists)
            {
                // Save the new user.
                user.googleAccessToken = authState.AccessToken;
                user.googleRefreshToken =
                        authState.RefreshToken == null ? "" : authState.RefreshToken;
                user.googleExpiresIn = (int)(authState.AccessTokenExpirationUtc.Value -
                        authState.AccessTokenIssueDateUtc.Value).TotalSeconds;
                user.googleExpiresAt = authState.AccessTokenExpirationUtc.Value;
                user.googleUserId = me.Id;
                user.googleDisplayName = me.DisplayName;
                user.googlePublicProfilePhotoUrl = me.Image.Url;
                user.googlePublicProfileUrl = me.Url;
                user.email = me.Emails == null ? "" : me.Emails[0].Value;

                db.Users.Add(user);
                db.SaveChanges();
                db.Entry(user);

                // Use the FriendsHelper to generate this user's list of friends
                // who also use this app.
                FriendsHelper friendHelper =
                    new FriendsHelper(HttpContext.Current.Request);

                // Generate the friends list asynchronously within a task.
                Action generateFriends = () => {
                    friendHelper.GenerateFriends(user, _authState);
                };
                Task.Factory.StartNew(generateFriends);

            }
            else
            {
                // Update the existing user's authorization state. Note that we aren't updating the
                // refresh token because it is only returned the first time the user authorizes the
                // app.
                user.googleAccessToken = authState.AccessToken;
                user.googleExpiresIn = (int)(authState.AccessTokenExpirationUtc.Value -
                        authState.AccessTokenIssueDateUtc.Value).TotalSeconds;
                user.googleExpiresAt = authState.AccessTokenExpirationUtc.Value;
                db.SaveChanges();
            }

            return user;
        }
コード例 #2
0
        /// <summary>
        /// Either:
        /// 1. Create a user for the given ID and credential
        /// 2. Or, update the existing user with the existing credential
        ///
        /// If 2, then ask Google for the user's public profile information to store.
        /// </summary>
        /// <param name="authState">The OAuth v2 state for authorizing the user.</param>
        /// <returns>A User object that represents the created user.</returns>
        public User SaveTokenForUser(IAuthorizationState authState)
        {
            // Set the authorization state in the superclass for the authorization call.
            _authState = authState;

            var provider = new WebServerClient(GoogleAuthenticationServer.Description);

            provider.ClientIdentifier = CLIENT_ID;
            provider.ClientSecret     = CLIENT_SECRET;
            var authenticator =
                new OAuth2Authenticator <WebServerClient>(
                    provider,
                    GetAuthorization)
            {
                NoCaching = true
            };

            ps = new PlusService(new BaseClientService.Initializer()
            {
                Authenticator = authenticator
            });

            Person me = ps.People.Get("me").Fetch();

            // Load the user model from the DB if the user already exists.
            bool             userExists = false;
            User             user       = new User();
            PhotohuntContext db         = new PhotohuntContext();
            User             existing   = db.Users.FirstOrDefault(u => u.googleUserId.Equals(me.Id));

            if (existing != null)
            {
                user       = existing;
                userExists = true;
            }

            if (!userExists)
            {
                // Save the new user.
                user.googleAccessToken  = authState.AccessToken;
                user.googleRefreshToken =
                    authState.RefreshToken == null ? "" : authState.RefreshToken;
                user.googleExpiresIn = (int)(authState.AccessTokenExpirationUtc.Value -
                                             authState.AccessTokenIssueDateUtc.Value).TotalSeconds;
                user.googleExpiresAt             = authState.AccessTokenExpirationUtc.Value;
                user.googleUserId                = me.Id;
                user.googleDisplayName           = me.DisplayName;
                user.googlePublicProfilePhotoUrl = me.Image.Url;
                user.googlePublicProfileUrl      = me.Url;
                user.email = me.Emails == null ? "" : me.Emails[0].Value;

                db.Users.Add(user);
                db.SaveChanges();
                db.Entry(user);

                // Use the FriendsHelper to generate this user's list of friends
                // who also use this app.
                FriendsHelper friendHelper =
                    new FriendsHelper(HttpContext.Current.Request);

                // Generate the friends list asynchronously within a task.
                Action generateFriends = () => {
                    friendHelper.GenerateFriends(user, _authState);
                };
                Task.Factory.StartNew(generateFriends);
            }
            else
            {
                // Update the existing user's authorization state. Note that we aren't updating the
                // refresh token because it is only returned the first time the user authorizes the
                // app.
                user.googleAccessToken = authState.AccessToken;
                user.googleExpiresIn   = (int)(authState.AccessTokenExpirationUtc.Value -
                                               authState.AccessTokenIssueDateUtc.Value).TotalSeconds;
                user.googleExpiresAt = authState.AccessTokenExpirationUtc.Value;
                db.SaveChanges();
            }

            return(user);
        }