public HttpResponseMessage SocialLogin(string LoginType, string UserName, string Secret, string AccessToken, string PushNotificationID, string MobilePlatform) { bool socialresult = false; object token = null; PenYourPrayerUser tuser = new PenYourPrayerUser(); if (LoginType.ToUpper() == "FACEBOOK") { socialresult = SocialMediaAuthentication.CheckFacebookAccessToken(AccessToken, ref token); if (socialresult) { FacebookDebugToken fbtoken = (FacebookDebugToken)token; tuser.ProfilePictureURL = fbtoken.data.usertoken.picture.data.url; tuser.DisplayName = fbtoken.data.usertoken.name; tuser.UserName = fbtoken.data.usertoken.email; } } //else if (LoginType.ToUpper() == "TWITTER") //{ // result = SocialMediaAuthentication.checkTwitterAccessToken(UserName, "806837785-trTr0ObdqaW0owy1N0WXJFh6OGSlgUH74nh3qoHO", "w5j7WPwHWwY4DSfJ82tRVZF7SBogZJ6XABptVt431uOowvwFKC"); //} else if (LoginType.ToUpper() == "GOOGLEPLUS") { socialresult = SocialMediaAuthentication.CheckGooglePlusAccessToken(AccessToken, ref token); if (socialresult) { GoogleTokenInfo gptoken = (GoogleTokenInfo)token; tuser.ProfilePictureURL = gptoken.picture; tuser.DisplayName = gptoken.name; tuser.UserName = gptoken.email; } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, new CustomResponseMessage() { StatusCode = (int)HttpStatusCode.BadRequest, Description = "Invalid Social Login" })); } if (!socialresult) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new CustomResponseMessage() { StatusCode = (int)HttpStatusCode.BadRequest, Description = "Invalid Social Login" })); } else { using (DBDataContext db = new DBDataContext()) { PenYourPrayerUser user; List <usp_GetUserInformationResult> res = db.usp_GetUserInformation(LoginType, UserName).ToList(); if (res.Count() > 0) { usp_GetUserInformationResult t = res.ElementAt(0); db.usp_UpdateUserMobileDeviceInformation(t.ID, MobilePlatform, PushNotificationID); db.usp_UpdateUserSocialInformation(t.ID, tuser.DisplayName, tuser.UserName, tuser.ProfilePictureURL); user = new PenYourPrayerUser(); user.ID = t.ID; user.DisplayName = tuser.DisplayName; user.LoginType = t.LoginType; user.UserName = t.UserName; user.MobilePlatform = MobilePlatform; user.ProfilePictureURL = tuser.ProfilePictureURL; user.PushNotificationID = PushNotificationID; user.HMACHashKey = t.HMACHashKey; user.EmailVerification = true; } else { //create new account string result = ""; string verificationCode = ""; long? id = -1; string HMACSecretKey = CustomPasswordHasher.HashPassword(Guid.NewGuid().ToString()) + CustomPasswordHasher.HashPassword(Guid.NewGuid().ToString()); db.usp_AddNewUser(LoginType, UserName, tuser.DisplayName, tuser.ProfilePictureURL, "", MobilePlatform, PushNotificationID, HMACSecretKey, null, null, null, ref result, ref id, ref verificationCode); user = new PenYourPrayerUser(); user.ID = (long)id; user.DisplayName = tuser.DisplayName; user.LoginType = LoginType; user.UserName = UserName; user.MobilePlatform = MobilePlatform; user.ProfilePictureURL = tuser.ProfilePictureURL; user.PushNotificationID = PushNotificationID; user.HMACHashKey = HMACSecretKey; user.EmailVerification = true; } return(Request.CreateResponse(HttpStatusCode.OK, user)); } } }
protected void Page_Load(object sender, EventArgs e) { Callback = Request.QueryString["callback"]; ContentTypeAlias = Request.QueryString["contentTypeAlias"]; PropertyAlias = Request.QueryString["propertyAlias"]; if (AuthState != null) { string[] stateValue = Session["Skybrud.Social_" + AuthState] as string[]; if (stateValue != null && stateValue.Length == 3) { Callback = stateValue[0]; ContentTypeAlias = stateValue[1]; PropertyAlias = stateValue[2]; } } // Get the prevalue options FacebookOAuthPreValueOptions options = FacebookOAuthPreValueOptions.Get(ContentTypeAlias, PropertyAlias); if (!options.IsValid) { Content.Text = "Hold on now! The options of the underlying prevalue editor isn't valid."; return; } // Configure the OAuth client based on the options of the prevalue options FacebookOAuthClient client = new FacebookOAuthClient { AppId = options.AppId, AppSecret = options.AppSecret, RedirectUri = options.RedirectUri }; // Session expired? if (AuthState != null && Session["Skybrud.Social_" + AuthState] == null) { Content.Text = "<div class=\"error\">Session expired?</div>"; return; } // Check whether an error response was received from Facebook if (AuthError != null) { Content.Text = "<div class=\"error\">Error: " + AuthErrorDescription + "</div>"; return; } // Redirect the user to the Facebook login dialog if (AuthCode == null) { // Generate a new unique/random state string state = Guid.NewGuid().ToString(); // Save the state in the current user session Session["Skybrud.Social_" + state] = new[] { Callback, ContentTypeAlias, PropertyAlias }; // Construct the authorization URL string url = client.GetAuthorizationUrl(state, options.Scope); // Redirect the user Response.Redirect(url); return; } // Exchange the authorization code for a user access token string userAccessToken; try { userAccessToken = client.GetAccessTokenFromAuthCode(AuthCode); } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to acquire access token</b><br />" + ex.Message + "</div>"; return; } try { // Initialize the Facebook service (no calls are made here) FacebookService service = FacebookService.CreateFromAccessToken(userAccessToken); // Make a call to the Facebook API to get information about the user FacebookUser me = service.Users.GetUser("me").Body; //Get accounts information. Only works with "manage_pages" permission FacebookAccountsResponse response = null; if (options.Scope.Contains("manage_pages")) { response = service.Accounts.GetAccounts(); } // Get debug information about the access token FacebookDebugToken debugToken = null; try { debugToken = service.Debug.DebugToken(userAccessToken).Body; } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to acquire debug token. Are you a developer?</b><br />" + ex.Message + "</div>"; } Content.Text += "<p>Hi <strong>" + me.Name + "</strong></p>"; Content.Text += "<p>Please wait while you're being redirected...</p>"; // Set the callback data FacebookOAuthData data = new FacebookOAuthData { Id = me.Id, Name = me.Name, AccessToken = userAccessToken, ExpiresAt = DateTime.Now.AddDays(60), BusinessPages = new FacebookBusinessPageData[0], SelectedBusinessPage = null }; // Set the the business pages (if available) if (response != null) { data.BusinessPages = response.Body.Data.Select(ac => new FacebookBusinessPageData { Id = ac.Id, Name = ac.Name, AccessToken = ac.AccessToken }).ToArray(); } // Update the OAuth data with information from the debug token if (debugToken != null) { data.ExpiresAt = (debugToken.Data.ExpiresAt ?? DateTime.Now.AddDays(60));//NULL when manage_pages permission is granted data.Scope = ( from scope in debugToken.Data.Scopes select scope.Name ).ToArray(); } // Update the UI and close the popup window Page.ClientScript.RegisterClientScriptBlock(GetType(), "callback", String.Format( "self.opener." + Callback + "({0}); window.close();", data.Serialize()//Save JSON data through callback parameter ), true); } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to get user information</b><br />" + ex.Message + "</div>"; } }
protected void Page_Load(object sender, EventArgs e) { Callback = Request.QueryString["callback"]; ContentTypeAlias = Request.QueryString["contentTypeAlias"]; PropertyAlias = Request.QueryString["propertyAlias"]; if (AuthState != null) { string[] stateValue = Session["Skybrud.Social_" + AuthState] as string[]; if (stateValue != null && stateValue.Length == 3) { Callback = stateValue[0]; ContentTypeAlias = stateValue[1]; PropertyAlias = stateValue[2]; } } // Get the prevalue options FacebookOAuthPreValueOptions options = FacebookOAuthPreValueOptions.Get(ContentTypeAlias, PropertyAlias); if (!options.IsValid) { Content.Text = "Hold on now! The options of the underlying prevalue editor isn't valid."; return; } // Configure the OAuth client based on the options of the prevalue options FacebookOAuthClient client = new FacebookOAuthClient { AppId = options.AppId, AppSecret = options.AppSecret, RedirectUri = options.RedirectUri }; // Session expired? if (AuthState != null && Session["Skybrud.Social_" + AuthState] == null) { Content.Text = "<div class=\"error\">Session expired?</div>"; return; } // Check whether an error response was received from Facebook if (AuthError != null) { Content.Text = "<div class=\"error\">Error: " + AuthErrorDescription + "</div>"; return; } // Redirect the user to the Facebook login dialog if (AuthCode == null) { // Generate a new unique/random state string state = Guid.NewGuid().ToString(); // Save the state in the current user session Session["Skybrud.Social_" + state] = new[] { Callback, ContentTypeAlias, PropertyAlias }; // Construct the authorization URL string url = client.GetAuthorizationUrl(state, "read_stream", "user_status", "user_about_me", "user_photos"); // Redirect the user Response.Redirect(url); return; } // Exchange the authorization code for a user access token string userAccessToken; try { userAccessToken = client.GetAccessTokenFromAuthCode(AuthCode); } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to acquire access token</b><br />" + ex.Message + "</div>"; return; } try { // Initialize the Facebook service (no calls are made here) FacebookService service = FacebookService.CreateFromAccessToken(userAccessToken); // Make a call to the Facebook API to get information about the user FacebookUser me = service.Users.GetUser("me").Body; // Get debug information about the access token FacebookDebugToken debug = service.Debug.DebugToken(userAccessToken).Body; Content.Text += "<p>Hi <strong>" + me.Name + "</strong></p>"; Content.Text += "<p>Please wait while you're being redirected...</p>"; // Set the callback data FacebookOAuthData data = new FacebookOAuthData { Id = me.Id, Name = me.Name, AccessToken = userAccessToken, ExpiresAt = debug.Data.ExpiresAt == null ? default(DateTime) : debug.Data.ExpiresAt.Value, Scope = ( from scope in debug.Data.Scopes select scope.Name ).ToArray() }; // Update the UI and close the popup window Page.ClientScript.RegisterClientScriptBlock(GetType(), "callback", String.Format( "self.opener." + Callback + "({0}); window.close();", data.Serialize() ), true); } catch (Exception ex) { Content.Text = "<div class=\"error\"><b>Unable to get user information</b><br />" + ex.Message + "</div>"; return; } }