我已经在我正在开发的 ASP.NET 网站中实现了 Facebook 登录和注册,但不确定我是否有最好的流程。欢迎任何意见/更正。我发现开发过程有点脱节...... facebook 为您提供了拼图的部分,但在我看来,这些部分如何组合在一起存在不足。我是这样做的:-
母版页:-
<!doctype html>
<%--facebook namespace needed for xfbml ...--%>
<html xmlns:fb="http://ogp.me/ns/fb#">
包含 facebook sdk 和 facebook login javascript 函数的脚本,该函数回发到 ASP.NET 应用程序,以根据我们数据库中的用户表检查 facebook 电子邮件。如您所见,facebook UserID 已被注释掉,因为我没有使用所使用的符号来提取它。(关于如何做到这一点的任何想法?): -
<%--the fb-root div is used by the facebook sdk and the like plugin further below the sdk load --%>
<div id="fb-root"></div>
<%--facebook javascript SDK--%>
<script type="text/javascript">
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXXXXXX', // App ID
channelUrl : '//'+window.location.hostname+'/channel.htm', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
}
// catch Facebook login button press
function FacebookLogin() {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function(response) {
// Do a post to the server to finish the logon
// This is a form post since we don't want to use AJAX
var form = document.createElement("form");
form.setAttribute("method", 'post');
form.setAttribute("action", '/RegistrationFacebookTestForUser.aspx');
// var field = document.createElement("input");
// field.setAttribute("type", "hidden");
// field.setAttribute("name", 'fbuserid');
// field.setAttribute("value", response.authResponse.userID);
// form.appendChild(field);
var field2 = document.createElement("input");
field2.setAttribute("type", "hidden");
field2.setAttribute("name", 'tempform_email');
field2.setAttribute("value", response.email.toString());
form.appendChild(field2);
document.body.appendChild(form);
form.submit();
});
}
</script>
表单发回 RegistrationFacebookTestForUser.aspx.cs 以对照我们的数据库检查用户的 Facebook 电子邮件:-
public partial class RegistrationFacebookTestForUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//fbid.Text = Request.Form["fbuserid"];
email.Text = Request.Form["tempform_email"];
// get user from facebook email
MembershipUser tempUser = Membership.GetUser(Request.Form["tempform_email"]);
if (tempUser == null) // if user not found redirect to Facebook Registration Page
{
Response.Redirect("~/RegistrationFacebook.aspx");
}
else // if user found set auth cookie and redirect
{
FormsAuthentication.SetAuthCookie(tempUser.UserName, true);
Response.Redirect(Request.UrlReferrer.ToString());
}
}
}
如果未找到电子邮件,请重定向到 RegistrationFacebook.aspx 以通过 Facebook 注册插件提供注册....
注册Facebook.aspx:-
<div id="facebook_registration" runat="server">
<iframe src="https://www.facebook.com/plugins/registration.php?
client_id=110835352327502&
redirect_uri=http://localhost/RegistrationFacebookBackend.aspx&
fields=
[
{'name':'name'},
{'name':'first_name'},
{'name':'last_name'},
{'name':'email'},
{'name':'password'},
{'name':'_business', 'description':'Do you work for ?', 'type':'select', 'options':{'':'I do not work for ','f5f064ad-0db4-42ce-ba86-e65e6d262768':'xxxxxxxx','534d6c7b-6c3a-4b7d-a41c-9e240c199f1d':'xxxxxxxx'}},
{'name':'network', 'description':'Please select your region', 'type':'select', 'options':{'a':'a','b':'b','c':'c','d':'d','e':'e','f':'f','g':'g','h':'h','i':'i','j':'j','k':'k','l':'l','96e9b78a-cc3b-4c6e-b4e4-bc0ba18184c1':'UK','521199a2-5847-4ec0-bca1-19052a110da8':'South Africa'}},
{'name':'public_profile', 'description':'Make my profile public so other users can see me', 'type':'checkbox', 'default':'checked'},
{'name':'newsletter', 'description':'Please send me the monthly Unite newsletter', 'type':'checkbox'},
{'name':'sponsor_email', 'description':'Please email me when someone sponsors me', 'type':'checkbox', 'default':'checked'},
{'name':'tandc', 'description':'I confirm that I have read and accepted the Terms and Conditions', 'type':'checkbox'}
]
"
scrolling="auto"
frameborder="no"
style="border:none"
allowTransparency="true"
width="100%"
height="530">
</iframe>
</div>
注册Facebook.aspx.cs :-
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
// its a save of content in admin mode
}
else
{
// RegistrationFacebookBackend has redirected back here after creating the user
if (!String.IsNullOrEmpty(Request.QueryString["facebook_result"]))
{
switch (Request.QueryString["facebook_result"])
{
case "Success":
CreateAccountResultsSuccess.Text = "You have successfully registered with Unite and the Unite Facebook app!";
CreateAccountResultsSuccess.Visible = true;
facebook_registration.Visible = false;
break;
case "InvalidUserName":
CreateAccountResultsError.Text = Resources.Error_Registration_InvalidUserName;
CreateAccountResultsError.Visible = true;
break;
case "DuplicateUserName":
CreateAccountResultsError.Text = Resources.Error_Registration_DuplicateUserName;
CreateAccountResultsError.Visible = true;
break;
case "DuplicateEmail":
CreateAccountResultsError.Text = Resources.Error_Registration_DuplicateEmail;
CreateAccountResultsError.Visible = true;
break;
case "InvalidEmail":
CreateAccountResultsError.Text = Resources.Error_Registration_InvalidEmail;
CreateAccountResultsError.Visible = true;
break;
case "InvalidPassword":
CreateAccountResultsError.Text = Resources.Error_Registration_InvalidPassword;
CreateAccountResultsError.Visible = true;
break;
default:
CreateAccountResultsError.Visible = true;
CreateAccountResultsError.Text = Resources.Error_Registration_Default;
break;
}
}
}
}
}
RegistrationFacebook.aspx 中的注册插件回发到 RegistrationFacebookBackend.aspx 以向 ASP.NET 应用程序注册用户。这里要关注的主要内容是 Page_Load 中的 facebook 表单 signed_request 变量解码以及成员资格和配置文件创建。我们使用 CachedMembershipProvider 但只是在这里替换普通的会员提供程序代码。(networkGUID 只是我们应用程序的一部分,因此请忽略它:-
public partial class RegistrationFacebookBackend : System.Web.UI.Page
{
private UserService userService;
private MembershipUser newUser;
private string userName; private string email; private string password; private string firstname; private string lastname;
private string _business; private string network; private string location;
private bool newsletter; private bool sponsor_email;
//private Guid networkId;
#region public properties
public UserService UserService
{
get { return userService ?? (userService = new UserService()); }
}
public MembershipUser NewUser
{
get { return newUser ?? (newUser = Membership.GetUser(userName)); }
}
#endregion
/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//
}
else // Request with Facebook signed_request payload ... create the user
{
if (!string.IsNullOrEmpty(Request.Form["signed_request"]))
{
string payload = Request.Form["signed_request"].Split('.')[1];
var encoding = new UTF8Encoding();
var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
var json = encoding.GetString(base64JsonArray);
var o = JObject.Parse(json);
userName = o.SelectToken("registration.email").ToString().Replace("\"", "");
email = o.SelectToken("registration.email").ToString().Replace("\"", "");
password = o.SelectToken("registration.password").ToString().Replace("\"", "");
firstname = o.SelectToken("registration.first_name").ToString().Replace("\"", "");
lastname = o.SelectToken("registration.last_name").ToString().Replace("\"", "");
_business = o.SelectToken("registration._business").ToString().Replace("\"", "");
network = o.SelectToken("registration.network").ToString().Replace("\"", "");
location = o.SelectToken("user.country").ToString().Replace("\"", "");
newsletter = (o.SelectToken("registration.newsletter").ToString().Replace("\"","") == "checked") ? true : false;
sponsor_email = (o.SelectToken("registration.sponsor_email").ToString().Replace("\"","") == "checked") ? true : false;
MembershipCreateStatus createStatus;
CachedMembershipProvider cmp = new CachedMembershipProvider();
System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
nvc.Add("providerName","AspNetSqlMembershipProvider");
cmp.Initialize("CachedMembershipProvider", nvc );
newUser = cmp.CreateUser(userName, password, email, null, null, true, Guid.NewGuid(), out createStatus);
switch (createStatus)
{
case MembershipCreateStatus.Success:
// set roles and create profile
SetUserProfile(true);
FormsAuthentication.SetAuthCookie(newUser.UserName, true);
Response.Redirect("RegistrationFacebook.aspx?facebook_result=Success");
break;
case MembershipCreateStatus.InvalidUserName:
Response.Redirect("RegistrationFacebook.aspx?facebook_result=InvalidUserName");
break;
case MembershipCreateStatus.DuplicateUserName:
Response.Redirect("RegistrationFacebook.aspx?facebook_result=DuplicateUserName");
break;
case MembershipCreateStatus.DuplicateEmail:
Response.Redirect("RegistrationFacebook.aspx?facebook_result=DuplicateEmail");
break;
case MembershipCreateStatus.InvalidEmail:
Response.Redirect("RegistrationFacebook.aspx?facebook_result=InvalidEmail");
break;
case MembershipCreateStatus.InvalidPassword:
Response.Redirect("RegistrationFacebook.aspx?facebook_result=InvalidPassword");
break;
default:
Response.Redirect("RegistrationFacebook.aspx?facebook_result=error_default");
break;
}
}
else // Request WITHOUT Facebook signed_request payload
{
}
}
}
/// <summary>
/// Gets the user profile.
/// </summary>
/// <param name="profileUserName">Name of the user.</param>
/// <returns></returns>
private Web.WebProfile GetUserProfile(string profileUserName)
{
ProfileBase wp = Web.WebProfile.Create(profileUserName);
return new Web.WebProfile(wp);
}
/// <summary>
/// Sets the user profile.
/// </summary>
private void SetUserProfile(bool createEvents)
{
if (newUser == null)
{
throw new ArgumentNullException("newUser");
}
//Control container = CreateUserWizard1.CreateUserStep.ContentTemplateContainer;
Web.WebProfile newProfile = GetUserProfile(NewUser.UserName);
newProfile.UserId = (Guid)NewUser.ProviderUserKey;
newProfile.FirstName = firstname;
newProfile.LastName = lastname;
newProfile.EmailAddress = email;
newProfile.NetworkId = NetworkGuid;
newProfile.FurtherInformationOptIn = true;
newProfile.Location = location;
newProfile.IsTemporary = false;
newProfile.OptInFavourites = false;
newProfile.OptInNewsletters = newsletter;
newProfile.OptInGiveTimeEvents = false;
newProfile.OptInFundraisers = sponsor_email;
newProfile.OptInNews = false;
newProfile.Save();
newProfile = new WebProfile(WebProfile.Create(NewUser.UserName));
UserService.CreateNewProfilePage(newProfile, false);
if (createEvents)
{
EventService ues = new EventService();
IEvent ue = ues.CreateEvent(EventType.Registration, newProfile.NetworkId, newProfile.UserId,
newProfile.UserId);
}
}
protected Guid NetworkGuid
{
get
{
Guid networkGuid;
if (Convertors.GuidTryParse(_business, out networkGuid) == true)
{
return networkGuid; // if they have selected a business that is the one to use
}
else if (Convertors.GuidTryParse(network, out networkGuid) == true)
{
return networkGuid; // otherwise use the region they have selected
}
else
{ //otherwise default to UK
Convertors.GuidTryParse("96e9b78a-cc3b-4c6e-b4e4-bc0ba18184c1", out networkGuid);
return networkGuid;
}
}
}
}