Пример #1
0
        public User LoginWithTwitter(string userName, string token, string tokenSecret)
        {

            // check to see if we already have the screen name in the userauth table.
            var userAuthService = new UserAuthenticationProfileService();
            var userAuth = userAuthService.UserAuthorizationByServiceScreenNameAndPlatform(userName, "Mobile", AuthenticationServices.TWITTER);

            //Yes, there is a Mobile Auth record
            if (userAuth != null)
            {
                userAuth.Token = token;
                userAuth.TokenSecret = tokenSecret;
                userAuthService.Save(userAuth);

                var user = userAuth.Users.FirstOrDefault();
                if (user != null)
                {
                    return user;
                }
            }
            else
            {
                //There is no auth record, this must be a login from Twitter or a new twitter connection to an existing user or an existing mobile user with no Mobile account.

                //Before we create a new user, make sure there isn't a Web token.
                userAuth = userAuthService.UserAuthorizationByServiceScreenNameAndPlatform(userName, "Web", AuthenticationServices.TWITTER);
                if (userAuth == null)
                {
                    //This is a new user, create an account.
                    var us = new UserService();
                    
                    //This is a new login, create the user
                    var theNewUser = new User()
                    {
                        CreatedDate = DateTime.UtcNow,
                        IsActive = true,
                        RoleID = 2,
                        Username = userName + "twitter",
                        Password = "******",
                        EmailAddress = " "
                    };
                    us.Save(theNewUser);
                    userAuth = new UserAuthenticationProfile
                    {
                        UserID = theNewUser.ID,
                        Platform = "Mobile",
                        Service = AuthenticationServices.TWITTER.ToString(),
                        ServiceUsername = userName,
                        Token = token,
                        TokenSecret = tokenSecret
                    };
                    userAuthService.Save(userAuth);

                    return theNewUser;
                }
                
                
                //They have an account already, it was create on Web. Now add the Mobile token.
                var userAuth2 = new UserAuthenticationProfile
                {
                    UserID = userAuth.UserID,
                    Platform = "Mobile",
                    Service = AuthenticationServices.TWITTER.ToString(),
                    ServiceUsername = userName,
                    Token = token,
                    TokenSecret = tokenSecret
                };
                userAuthService.Save(userAuth2);

                var user = userAuth.Users.FirstOrDefault();
                return user;

            }

            return null;
            
        }
Пример #2
0
        private static void ConnectAuthAccountToUser(CreateAccountModel model, User user)
        {
            //Store the auth tokens for whatever service
            var userAuth = new UserAuthenticationProfile
            {
                UserID = user.ID,
                Platform = "Web",
                ServiceUsername = model.AuthScreenname,
                Token = model.AuthToken,
                TokenSecret = model.AuthTokenSecret
            };

            switch (model.AuthService)
            {
                case "twitter":
                    {
                        userAuth.Service = AuthenticationServices.TWITTER.ToString();
                        break;
                    }
                case "facebook":
                    {
                        userAuth.Service = AuthenticationServices.FACEBOOK.ToString();
                        break;
                    }
            }

            new UserAuthenticationProfileService().Save(userAuth);
        }