コード例 #1
0
        public User CreateOrUpdate(OAuthData oAuthData, string userName, string fullName, string email)
        {
            // Lets find an existing user for the provider OR the email address if the provider doesn't exist.
            User user = DocumentSession
                            .Query<User>()
                            .SingleOrDefault(x =>
                                             x.OAuthData.Any(y => y.Id == oAuthData.Id &&
                                                                  y.OAuthProvider == oAuthData.OAuthProvider)) ??
                        DocumentSession
                            .Query<User>()
                            .SingleOrDefault(x => x.Email == email);

            if (user != null)
            {
                // User exists, so lets update the OAuth data, for this user.
                if (user.OAuthData != null)
                {
                    OAuthData existingProvider =
                        user.OAuthData.SingleOrDefault(x => x.OAuthProvider == oAuthData.OAuthProvider);
                    if (existingProvider != null)
                    {
                        user.OAuthData.Remove(existingProvider);
                    }
                }
                else
                {
                    user.OAuthData = new List<OAuthData>();
                }

                user.OAuthData.Add(oAuthData);
            }
            else
            {
                // Ok. No user at all. We create one and store it.
                user = new User
                           {
                               DisplayName = userName,
                               Email = email,
                               Id = null,
                               FullName = fullName,
                               CreatedOn = DateTime.UtcNow,
                               IsActive = true,
                               OAuthData = new List<OAuthData>(),
                               FavoriteTags = new List<string> {"ravendb", "c#", "asp.net-mvc3"}
                           };
                user.OAuthData.Add(oAuthData);
            }

            DocumentSession.Store(user);

            return user;
        }
コード例 #2
0
        public User CreateOrUpdate(OAuthData oAuthData, string userName, string fullName, string email)
        {
            // First, lets see if we have a user with this id, for this provider.
            User existingUser = _documentSession.Query<User>()
                .Where(
                    x =>
                    x.OAuthData.Any(y => y.Id == oAuthData.Id && y.OAuthProvider == oAuthData.OAuthProvider))
                .SingleOrDefault();

            if (existingUser != null)
            {
                // User exist. All is good :)
                return existingUser;
            }

            // No user exists for the OAuth provider and Id. So lets try their email address.
            existingUser = _documentSession.Query<User>()
                .Where(x => x.Email == email)
                .SingleOrDefault();

            if (existingUser != null)
            {
                // User exist, but isn't associated to this OAuthProvider. So lets do that!
                if (existingUser.OAuthData == null)
                {
                    existingUser.OAuthData = new List<OAuthData>();
                }
                existingUser.OAuthData.Add(oAuthData);
                _documentSession.Store(existingUser);
                _documentSession.SaveChanges();
                return existingUser;
            }

            // Ok. No user at all. We create one and store it.
            var newUser = new User
                              {
                                  DisplayName = userName,
                                  Email = email,
                                  Id = null,
                                  FullName = fullName,
                                  OAuthData = new List<OAuthData>()
                              };
            newUser.OAuthData.Add(oAuthData);

            _documentSession.Store(newUser);
            _documentSession.SaveChanges();

            return newUser;
        }
コード例 #3
0
ファイル: UsersController.cs プロジェクト: neiz/RavenOverflow
        private Core.Entities.User AcceptFacebookOAuthToken(FacebookOAuthResult facebookOAuthResult)
        {
            Condition.Requires(facebookOAuthResult).IsNotNull();

            // Grab the code.
            string code = facebookOAuthResult.Code;

            // Grab the access token.
            FacebookOAuthClient facebookOAuthClient = FacebookOAuthClient;
            dynamic result = facebookOAuthClient.ExchangeCodeForAccessToken(code);

            var oauthData = new OAuthData
            {
                OAuthProvider = OAuthProvider.Facebook,
                AccessToken = result.access_token,
                ExpiresOn = DateTime.UtcNow.AddSeconds(result.expires)
            };

            // Now grab their info.
            var facebookWebClient = new FacebookWebClient(oauthData.AccessToken);
            dynamic facebookUser = facebookWebClient.Get("me");
            oauthData.Id = facebookUser.id;

            // Not sure how to Inject an IUserService because it requires a Session .. which I don't have.
            var userService = new UserService(DocumentSession);

            // Now associate this facebook user to an existing user OR create a new one.
            return userService.CreateOrUpdate(oauthData, facebookUser.username, facebookUser.name, facebookUser.email);
        }
コード例 #4
0
        private ActionResult AcceptFacebookOAuthToken(FacebookOAuthResult facebookOAuthResult)
        {
            Condition.Requires(facebookOAuthResult).IsNotNull();

            // Grab the code.
            var code = facebookOAuthResult.Code;

            // Grab the access token.
            var facebookOAuthClient = FacebookOAuthClient;
            dynamic result = facebookOAuthClient.ExchangeCodeForAccessToken(code);

            OAuthData oauthData = new OAuthData
                                         {
                                             OAuthProvider = OAuthProvider.Facebook,
                                             AccessToken = result.access_token,
                                             ExpiresOn = DateTime.UtcNow.AddSeconds(result.expires)
                                         };

            // Now grab their info.
            var facebookWebClient = new FacebookWebClient(oauthData.AccessToken);
            dynamic facebookUser = facebookWebClient.Get("me");
            oauthData.Id = facebookUser.id;

            // Now associate this facebook user to an existing user OR create a new one.
            UserService userService = new UserService(DocumentSession);
            var user = userService.CreateOrUpdate(oauthData, facebookUser.username, facebookUser.name, facebookUser.email);

            return Json(user, JsonRequestBehavior.AllowGet);
        }