/// <summary> /// Logs a member and returns authentication token /// </summary> /// <param name="apiMember"></param> /// <param name="facebookKey"></param> /// <returns></returns> /// public Authentication GetAuthenticatedMemberLoginToken(ApiAccess apiMember, string facebookKey) { if (String.IsNullOrEmpty(facebookKey)) { return GetBadAuthentication("Missing or Invalid facebook key"); } Authentication auth = null; try { var memberRepo = new MemberRepository(); var member = memberRepo.GetByFacebookKey(facebookKey); if (null == member || !member.IsActive) { return GetBadAuthentication("Member not found or is inactive"); } const string hoursAhead = "1000"; var sessionEnds = DateTime.Now.AddHours(Convert.ToDouble(hoursAhead)).Ticks; var plainAuthKey = String.Concat(Convert.ToString(member.MemberId), doubleUC, Convert.ToString(sessionEnds), doubleUC, apiMember.AppKey); var generatedAuthKey = plainAuthKey.EncryptSymmetric<RijndaelManaged>(encryptPass, encryptSalt); auth = new Authentication {Reason = String.Empty, Success = true, AuthKey = generatedAuthKey}; } catch (Exception e) { auth = GetBadAuthentication("Invalid facebook key and/or member not found"); } return auth; }
/* [HttpGet] public ActionResult FacebookLogin(string token) { var client = new WebClient(); string JsonResult = client.DownloadString(string.Concat("https://graph.facebook.com/me?access_token=", token)); var jsonUserInfo = JObject.Parse(JsonResult); // you can get more user's info here. Please refer to: // http://developers.facebook.com/docs/reference/api/user/ string username = jsonUserInfo.Value<string>("username"); string email = jsonUserInfo.Value<string>("email"); string locale = jsonUserInfo.Value<string>("locale"); string facebook_userID = jsonUserInfo.Value<string>("id"); // store user's information here... FormsAuthentication.SetAuthCookie(username, true); return RedirectToAction("Index", "Home"); } */ public ActionResult FacebookCallback(string code) { var fb = new FacebookClient(); dynamic result = fb.Post("oauth/access_token", new { client_id = BananaSplit.Core.Utility.Config.FacebookId, client_secret = BananaSplit.Core.Utility.Config.FacebookSecret, redirect_uri = RedirectUri.AbsoluteUri, code = code }); var accessToken = result.access_token; // update the facebook client with the access token so // we can make requests on behalf of the user fb.AccessToken = accessToken; // Get the user's information dynamic me = fb.Get("me?fields=first_name,last_name,id,email"); string email = me.email; string fbId = me.id; var now = DateTime.Now; var memberRepo = new MemberRepository(); var member = memberRepo.GetByFacebookKey(fbId); if (null == member) { member = new Member(); member.FirstName = me.first_name; member.LastName = me.last_name; member.Email = email; member.FacebookId = fbId; member.DateCreated = now; member.MemberTypeId = 1; } member.DateUpdated = now; member.DateLastAccessed = now; memberRepo.Save(member); //TODO: Check via email to see if this user is authorized to be added. NEED A PAGE FOR THIS // Store the access token in the session Session["FBAccessToken"] = accessToken; Session["FBId"] = fbId; // Set the auth cookie //FormsAuthentication.SetAuthCookie(email, false); return RedirectToAction("Index", "Home"); }
public ActionResult RegisterFacebookMember(string appKey, string facebookKey, string firstName, string lastName, string email, string deviceId, string mobilePhoneNumber = null, string phoneModel = "iPhone") { var apiAccessRepo = new ApiAccessRepository(); var apiAccess = apiAccessRepo.GetMemberByPublicApiKey(appKey); var apiAuthResult = new ApiAuthResult(); if (null != apiAccess) { try { var memberRepo = new MemberRepository(); var now = DateTime.Now; var member = memberRepo.GetByFacebookKey(facebookKey); if (null == member) { member = new Member(); member.FirstName = firstName; member.LastName = lastName; member.Email = email; member.FacebookId = facebookKey; member.DateCreated = now; member.MemberTypeId = 1; } member.DateUpdated = now; member.DateLastAccessed = now; memberRepo.Save(member); //Now Get User Auth Token var authService = new AuthenticationService(); var auth = authService.GetAuthenticatedMemberLoginToken(apiAccess, member); apiAuthResult.Success = auth.Success; apiAuthResult.Description = auth.Reason; apiAuthResult.AuthKey = auth.AuthKey; }catch(Exception e) { apiAuthResult.Success = false; apiAuthResult.Description = "Failed to save member and/or generate auth token"; apiAuthResult.AuthKey = ""; } }else { apiAuthResult.Success = false; apiAuthResult.Description = "Invalid AppKey"; apiAuthResult.AuthKey = ""; } return this.ToXml(apiAuthResult); }