public ActionResult AuthTwitter() { // http://stackoverflow.com/questions/3599621/is-there-a-way-to-get-an-users-email-id-after-verifying-her-twitter-identity-us TwitterConsumer.TwitterConsumerKey = AppSession.Parameters.SocialTwitterConsumerKey.Value; TwitterConsumer.TwitterConsumerSecret = AppSession.Parameters.SocialTwitterConsumerSecret.Value; String RedirectTo = ""; if (TwitterConsumer.IsTwitterConsumerConfigured) { string screenName; int userId; string AccessToken; if (TwitterConsumer.TryFinishSignInWithTwitter(out screenName, out userId, out AccessToken)) { // In a real app, the Twitter username would likely be used // to log the user into the application. String memberName = String.Format("{0}@twitter.com",screenName); Member member = Members.GetByEmail(memberName); if (member.MemberID <= 0) { member.Name = screenName; member.Email = memberName; member.Password = StringTool.RandomString(24); member.TimeZoneID = ""; member.Save(); MemberSocialNetwork memberNetwork = new MemberSocialNetwork(); memberNetwork.MemberID = member.MemberID; memberNetwork.SocialNetworkID = 2; memberNetwork.SocialNetworkUserID = userId.ToString(); memberNetwork.SocialNetworkUserEmail = ""; memberNetwork.SocialNetworkUserName = screenName; memberNetwork.AccessToken = AccessToken; memberNetwork.Save(); if (member.MemberID > 0) { string[] RoleNames = AppParams.SocialTwitterRoles.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (String RoleName in RoleNames) { Role role = Web.Admin.Logic.Collections.Roles.GetBy(RoleName.Trim()); if (role.RoleID > 0) { MemberRole memberrole = new MemberRole(); memberrole.MemberID = member.MemberID; memberrole.RoleID = role.RoleID; memberrole.Save(); } } } } else { member.UpdateLoginTime(); MemberSocialNetwork memberNetwork = MemberSocialNetworks.GetByMemberID(member.MemberID, 2, userId.ToString()); memberNetwork.SocialNetworkID = 2; memberNetwork.SocialNetworkUserID = userId.ToString(); memberNetwork.SocialNetworkUserEmail = ""; memberNetwork.SocialNetworkUserName = screenName; memberNetwork.AccessToken = AccessToken; if (memberNetwork.MemberID <= 0) { memberNetwork.MemberID = member.MemberID; memberNetwork.Save(); } else memberNetwork.UpdateAccessToken(); } Domain _domain = Domains.GetByName(AppSession.SignUpDomain); if (_domain.DomainID > 0) { MemberDomain _memberDomain = new MemberDomain(); _memberDomain.DomainID = _domain.DomainID; _memberDomain.MemberID = member.MemberID; _memberDomain.Save(); } RedirectTo = SignInMember(memberName, member, false, "", "SSI via Twitter."); } else { return TwitterConsumer.StartSignInWithTwitter(true).AsActionResultMvc5(); } } return new RedirectResult(RedirectTo); }
private ActionResult AuthFacebook() { // http://developers.facebook.com/docs/reference/api/user/ // https://developers.facebook.com/docs/howtos/login/server-side-re-auth/ String RedirectTo = ""; IAuthorizationState authorization = AppSession.AppFacebookClient.ProcessUserAuthorization(Request); if (authorization == null) { // https://developers.facebook.com/docs/reference/login/#permissions var scope = new List<string>(); scope.Add("email"); scope.Add("user_likes"); scope.Add("read_stream"); scope.Add("user_online_presence"); scope.Add("read_stream"); // Kick off authorization request return AppSession.AppFacebookClient.PrepareRequestUserAuthorization().AsActionResultMvc5(); } else { var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken) + "&fields=id,name,first_name,last_name,username,email,gender,locale,middle_name,timezone"); using (var response = request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { var graph = FacebookGraph.Deserialize(responseStream); var name = HttpUtility.HtmlEncode(graph.Name); var firstName = HttpUtility.HtmlEncode(graph.FirstName); var lastName = HttpUtility.HtmlEncode(graph.LastName); // to log the user into the application. String memberName = String.Format("{0}.{1}@facebook.com", firstName, lastName); Member member = Members.GetByEmail(memberName); if (member.MemberID <= 0) { member.Name = String.Format("{0} {1}", firstName, lastName); member.Email = memberName; member.Password = StringTool.RandomString(24); member.TimeZoneID = ""; member.Save(); MemberSocialNetwork memberNetwork = new MemberSocialNetwork(); memberNetwork.MemberID = member.MemberID; memberNetwork.SocialNetworkID = 1; memberNetwork.SocialNetworkUserID = graph.Id.ToString(); //memberNetwork.SocialNetworkUserEmail = graph.Email; memberNetwork.SocialNetworkUserName = member.Name; memberNetwork.AccessToken = authorization.AccessToken; memberNetwork.Save(); if (member.MemberID > 0) { string[] RoleNames = AppParams.SocialFacebookRoles.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (String RoleName in RoleNames) { Role role = Web.Admin.Logic.Collections.Roles.GetBy(RoleName.Trim()); if (role.RoleID > 0) { MemberRole memberrole = new MemberRole(); memberrole.MemberID = member.MemberID; memberrole.RoleID = role.RoleID; memberrole.Save(); } } } } else { member.UpdateLoginTime(); MemberSocialNetwork memberNetwork = MemberSocialNetworks.GetByMemberID(member.MemberID, 1, graph.Id.ToString()); memberNetwork.SocialNetworkID = 1; memberNetwork.SocialNetworkUserID = graph.Id.ToString(); //memberNetwork.SocialNetworkUserEmail = graph.Email; memberNetwork.SocialNetworkUserName = member.Name; memberNetwork.AccessToken = authorization.AccessToken; if (memberNetwork.MemberID <= 0) { memberNetwork.MemberID = member.MemberID; memberNetwork.Save(); } else memberNetwork.UpdateAccessToken(); } Domain _domain = Domains.GetByName(AppSession.SignUpDomain); if (_domain.DomainID > 0) { MemberDomain _memberDomain = new MemberDomain(); _memberDomain.DomainID = _domain.DomainID; _memberDomain.MemberID = member.MemberID; _memberDomain.Save(); } RedirectTo = SignInMember(memberName, member, false, "", "SSI via Facebook."); return new RedirectResult(RedirectTo); } } } // return new EmptyResult(); }