nopCommerce_1.90_Source\NopCommerceStore\FBUtils.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FBUtils.aspx.cs" Inherits="NopSolutions.NopCommerce.Web.FBUtils" %>
nopCommerce_1.90_Source\NopCommerceStore\FBUtils.aspx.cs
using System;
using System.Configuration;
using System.Web.Services;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
using Facebook;
using System.Web.Security;
using NopSolutions.NopCommerce.Common;
using System.Web.UI;
using System.Web;
using NopSolutions.NopCommerce.Common.Utils;
using PayPal.Payments.DataObjects;
using System.Text;
using NopSolutions.NopCommerce.BusinessLogic;
namespace NopSolutions.NopCommerce.Web
{
public partial class FBUtils : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string UserIDExists(string uid, string accesstoken, string returnurl)
{
string result = "false";
var facebokClient = string.IsNullOrEmpty(accesstoken) ? new FacebookClient() : new FacebookClient(accesstoken);
dynamic me = facebokClient.Get("me?fields=email");
string email = me.email;
Customer customer = IoC.Resolve<ICustomerService>().GetCustomerByFacebookId(uid);
if (customer == null)
{
customer = IoC.Resolve<ICustomerService>().GetCustomerByEmail(email);
if (customer != null)
{
//However the customer with the same email exits, we combine them together
customer.FacebookId = uid;
IoC.Resolve<ICustomerService>().UpdateCustomer(customer);
}
else
{
// this customer does not exist, we add this customer to our login system
Customer facebookCustomer = new Customer();
MembershipCreateStatus createStatus = MembershipCreateStatus.Success;
IoC.Resolve<ICustomerService>().AddCustomer(email, uid, email, string.Empty, false, false, true, out createStatus);
if (createStatus != MembershipCreateStatus.Success)
{
result = "false";
//throw new NopException(string.Format("Could not create new customer: {0}", createStatus.ToString()));
}
}
}
else
{
// need to combine this with the existing customer in our db
}
result = "true";
if (string.Equals(result, "true", StringComparison.OrdinalIgnoreCase))
{
// login this facebook user into our system
IoC.Resolve<ICustomerService>().Login(email);
FormsAuthentication.SetAuthCookie(email, false);
// facebook login succeeded, we need a flag to indicate that this is a facebook login so that if user click
// logout we will log facebook out
NopContext.IsFacebookLogin = true;
}
if (String.IsNullOrEmpty(returnurl))
{
returnurl = CommonHelper.GetStoreLocation();
}
else
{
returnurl = HttpUtility.UrlDecode(returnurl);
}
StringBuilder oBuilder = new StringBuilder();
oBuilder.Append("{");
oBuilder.AppendFormat(@"""{0}"":""{1}"",", "result", result);
oBuilder.AppendFormat(@"""{0}"":""{1}""", "returnUrl", returnurl);
oBuilder.Append("}");
return oBuilder.ToString();
}
}
}
Remember the web service has to have [WebMethod] attribute, and has to be a static method
in this method, you will see all the functions I defined in previous posts get called.
When we call the webservice UserIDExists (see previous post), we pass in a uid, a accesstoken and a returnurl
In UserIDExists, we use the access token to retrieve the email of this facebook user; and use uid as the FacebookId of
this user.
If the customer with the specified facebook id does not exist in our system, but the customer with the same email already in our system, we combine them together :
customer.FacebookId = uid;
IoC.Resolve<ICustomerService>().UpdateCustomer(customer);
If the customer with the specified facebook id does not exist in our system, and there is no customer with the same email in our system(meaning this is a brand new customer), we add this customer to our login system.
Customer facebookCustomer = new Customer();
MembershipCreateStatus createStatus = MembershipCreateStatus.Success;
IoC.Resolve<ICustomerService>().AddCustomer(email, uid, email, string.Empty, false, false, true, out createStatus);
If we add this facebook user into our login system successfully, we then login this facebook user into our system by:
// login this facebook user into our system
IoC.Resolve<ICustomerService>().Login(email);
FormsAuthentication.SetAuthCookie(email, false);
// facebook login succeeded, we need a flag to indicate that this is a facebook login so that if user click
// logout we will log facebook out
NopContext.IsFacebookLogin = true;
FormsAuthentication.SetAuthCookie(email, false); is the magic here. Note that I haven't figured out how to
detect 'remember me' checkbox on the facebook login popup. The behavior i am using always act as if remeber me is checked. You can tell me if you find solution to detect 'remember me' check box and thus set SetAuthCookie accordingly.
After the facebook login successfully, i save a flag in a static global member
NopContext.IsFacebookLogin = true;
IsFacebookLogin is defined in nopCommerce_1.90_Source\Libraries\Nop.BusinessLogic\NopContext.cs
public static bool IsFacebookLogin;
We need this information when the user log out:
if this user is a facebook user, we need to do facebook log out.
Again, using IsFacebookLogin is just one way (maybe not so good way) to save the state. I just pick one to make everything work first, so apparently the above code also need some refactoring in the future.
The webservice returns a jason object which contains a result and a return url, the jason object is used in ajax response:
success: function (data) {
var obj = jQuery.parseJSON(data.d);
if (obj['result'] == "true") {
response.session["isfacebooklogin"] = "true";
window.location = obj['returnUrl'];
}
}
I will finish things up in the next post due to the length limit of each post.