Example #1
0
    private static void UpdateUsersAuthenticationToken(string newToken) {
        var service= new UserService();
        var authService = new UserAuthenticationProfileService();

        // get the current userid
        Guid uid;
        Guid.TryParse(CookieHelpers.GetCookieValue("lc", "uid").ToString(), out uid);
         
        // get the users authentication record and update the token
        var user = service.GetUserByID(uid);
        var userAuth = user.UserAuthenticationProfiles.FirstOrDefault(ua => ua.Service == "FACEBOOK");
        userAuth.Token = newToken;

        authService.Save(userAuth);
    }
Example #2
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;
            
        }
Example #3
0
        private Guid CheckConnectedAccountUserExists(string userScreenName, AuthenticationServices authService, string token, string tokenSecret)
        {
            try
            {

                var userAuthService = new UserAuthenticationProfileService();
                var userAuth = userAuthService.UserAuthorizationByServiceScreenNameAndPlatform(userScreenName, "Web", authService);
                if (userAuth != null)
                {
                    string logInMethod = null;
                    switch (authService)
                    {
                        case AuthenticationServices.TWITTER:
                            userAuth.Token = token;
                            userAuth.TokenSecret = tokenSecret;
                            logInMethod = "Twitter";
                            break;
                        case AuthenticationServices.FACEBOOK:
                            userAuth.Token = token;
                            logInMethod = "Facebook";
                            break;
                    }
                    userAuthService.Save(userAuth);

                    var user = userAuth.Users.FirstOrDefault();
                    if (user != null)
                    {

                        //Record the Login
                        var ut = new UserLoginTracking()
                        {
                            UserId = user.ID,
                            LoginMethod = logInMethod,
                            DateTime = DateTime.UtcNow,
                            IPAddress = HttpContext.Request.UserHostAddress
                        };
                        new UserLoginTrackingService().Save(ut);
                        return user.ID;
                    }
                }

                return Guid.Empty;
            }
            catch (Exception)
            {
                return Guid.Empty;
            }
        }