示例#1
0
        protected void btnChange_Click( object sender, EventArgs e )
        {
            UserService userService = new UserService();
            User user = userService.GetByUserName( tbUserName.Text );
            if ( user != null )
            {
                if ( userService.ChangePassword( user, tbOldPassword.Text, tbPassword.Text ) )
                {
                    userService.Save( user, CurrentPersonId );

                    lSuccess.Text = AttributeValue( "SuccessCaption" );
                    pnlEntry.Visible = false;
                    pnlSuccess.Visible = true;
                }
                else
                {
                    lInvalid.Text = AttributeValue( "InvalidPasswordCaption" );
                    pnlInvalid.Visible = true;
                }
            }
            else
            {
                lInvalid.Text = AttributeValue( "InvalidUserNameCaption" );
                pnlInvalid.Visible = true;
            }
        }
示例#2
0
        /// <summary>
        /// Gets the current user.
        /// </summary>
        /// <param name="userIsOnline">if set to <c>true</c> [user is online].</param>
        /// <returns></returns>
        public static User GetCurrentUser(bool userIsOnline)
        {
            string userName = User.GetCurrentUserName();

            if (userName != string.Empty)
            {
                if (userName.StartsWith("rckipid="))
                {
                    Rock.CRM.PersonService personService      = new CRM.PersonService();
                    Rock.CRM.Person        impersonatedPerson = personService.GetByEncryptedKey(userName.Substring(8));
                    if (impersonatedPerson != null)
                    {
                        return(impersonatedPerson.ImpersonatedUser);
                    }
                }
                else
                {
                    UserService userService = new UserService();
                    User        user        = userService.GetByUserName(userName);

                    if (user != null && userIsOnline)
                    {
                        user.LastActivityDate = DateTime.Now;
                        userService.Save(user, null);
                    }

                    return(user);
                }
            }

            return(null);
        }
示例#3
0
        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public static bool Validate( string username, string password )
        {
            UserService service = new UserService();
            User user = service.GetByUserName( username );
            if ( user != null )
                return service.Validate( user, password );

            return false;
        }
示例#4
0
 public bool Available( string username )
 {
     using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
     {
         uow.objectContext.Configuration.ProxyCreationEnabled = false;
         Rock.CMS.UserService UserService = new Rock.CMS.UserService();
         User User = UserService.GetByUserName( username );
         return ( User == null );
     }
 }
示例#5
0
 public bool Available(string username)
 {
     using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
     {
         uow.objectContext.Configuration.ProxyCreationEnabled = false;
         Rock.CMS.UserService UserService = new Rock.CMS.UserService();
         User User = UserService.GetByUserName(username);
         return(User == null);
     }
 }
示例#6
0
        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public static bool Validate(string username, string password)
        {
            UserService userService = new UserService();
            User        user        = userService.GetByUserName(username);

            if (user != null)
            {
                return(userService.Validate(user, password));
            }

            return(false);
        }
示例#7
0
        /// <summary>
        /// Gets the current user.
        /// </summary>
        /// <param name="userIsOnline">if set to <c>true</c> [user is online].</param>
        /// <returns></returns>
        public static User GetCurrentUser( bool userIsOnline )
        {
            UserService userService = new UserService();
            User user = userService.GetByUserName( User.GetCurrentUserName() );

            if ( user != null && userIsOnline )
            {
                user.LastActivityDate = DateTime.Now;
                userService.Save( user, null );
            }

            return user;
        }
示例#8
0
        /// <summary>
        /// Awaits permission of facebook user and will issue authenication cookie if successful.
        /// </summary>
        /// <param name="code">Facebook authorization code</param>
        /// <param name="state">Redirect url</param>
        private void ProcessOAuth( string code, string state )
        {
            FacebookOAuthResult oAuthResult;

            if ( FacebookOAuthResult.TryParse( Request.Url, out oAuthResult ) && oAuthResult.IsSuccess )
            {
                try
                {
                    // create client to read response
                    var oAuthClient = new FacebookOAuthClient( FacebookApplication.Current ) { RedirectUri = new Uri( GetOAuthRedirectUrl() ) };
                    oAuthClient.AppId = PageInstance.Site.FacebookAppId;
                    oAuthClient.AppSecret = PageInstance.Site.FacebookAppSecret;
                    dynamic tokenResult = oAuthClient.ExchangeCodeForAccessToken( code );
                    string accessToken = tokenResult.access_token;

                    FacebookClient fbClient = new FacebookClient( accessToken );
                    dynamic me = fbClient.Get( "me" );
                    string facebookId = "FACEBOOK_" + me.id.ToString();

                    // query for matching id in the user table
                    UserService userService = new UserService();
                    var user = userService.GetByUserName( facebookId );

                    // if not user was found see if we can find a match in the person table
                    if ( user == null )
                    {
                        try
                        {
                            // determine if we can find a match and if so add an user login record

                            // get properties from Facebook dynamic object
                            string lastName = me.last_name.ToString();
                            string firstName = me.first_name.ToString();
                            string email = me.email.ToString();

                            var personService = new PersonService();
                            var person = personService.Queryable().FirstOrDefault( u => u.LastName == lastName && (u.GivenName == firstName || u.NickName == firstName) && u.Email == email );

                            if ( person != null )
                            {
                                // since we have the data enter the birthday from Facebook to the db if we don't have it yet
                                DateTime birthdate = Convert.ToDateTime( me.birthday.ToString() );

                                if ( person.BirthDay == null )
                                {
                                    person.BirthDate = birthdate;
                                    personService.Save( person, person.Id );
                                }

                            }
                            else
                            {
                                person = new Person();
                                person.GivenName = me.first_name.ToString();
                                person.LastName = me.last_name.ToString();
                                person.Email = me.email.ToString();

                                if (me.gender.ToString() == "male")
                                    person.Gender = Gender.Male;
                                if (me.gender.ToString() == "female")
                                    person.Gender = Gender.Female;

                                person.BirthDate = Convert.ToDateTime( me.birthday.ToString() );

                                personService.Add( person, null );
                                personService.Save( person, null );
                            }

                            user = userService.Create( person, AuthenticationType.Facebook, facebookId, "fb", true, person.Id );
                        }
                        catch ( Exception ex )
                        {
                            string msg = ex.Message;
                            // TODO: probably should report something...
                        }

                        // TODO: Show label indicating inability to find user corresponding to facebook id
                    }

                    // update user record noting the login datetime
                    user.LastLoginDate = DateTime.Now;
                    user.LastActivityDate = DateTime.Now;
                    userService.Save( user, user.PersonId );

                    FormsAuthentication.SetAuthCookie( user.UserName, false );

                    if ( state != null )
                        Response.Redirect( state );

                }
                catch ( FacebookOAuthException oae )
                {
                    string msg = oae.Message;
                    // TODO: Add error handeling
                    // Error validating verification code. (usually from wrong return url very picky with formatting)
                    // Error validating client secret.
                    // Error validating application.
                }
            }
        }