private void DisplayDuplicates( Direction direction ) { bool displayed = false; if ( Convert.ToBoolean( AttributeValue( "Duplicates" ) ) ) { PersonService personService = new PersonService(); var matches = personService. Queryable(). Where( p => p.Email.ToLower() == tbEmail.Text.ToLower() && p.LastName.ToLower() == tbLastName.Text.ToLower() ). ToList(); if ( matches.Count > 0 ) { gDuplicates.AllowPaging = false; gDuplicates.ShowActionRow = false; gDuplicates.DataSource = matches; gDuplicates.DataBind(); ShowPanel( 1 ); displayed = true; } else displayed = false; } if ( !displayed ) { if ( direction == Direction.Forward ) DisplaySuccess( CreateUser (CreatePerson(), true)); else DisplayUserInfo( direction ); } }
/// <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. } } }