防止重复用户添加到数据库中
我想防止同一用户自动添加到数据库中。我想根据数据库中设置的 FacebookID
字段/列进行检查。我该怎么做?下面是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCFacebookTestApp.Models;
using Facebook;
using Facebook.Web;
namespace MVCFacebookTestApp.Controllers
{
public class HomeController : Controller
{
public FacebookSession FacebookSession
{
get { return (FacebookWebContext.Current.Session); }
}
public ActionResult Index()
{
string request = Request.Form["signed_request"];
string accessToken = "";
if (Request.Form["access_token"] != null)
{
accessToken = Request.Form["access_token"];
}
FacebookApplication app = new FacebookApplication();
FacebookSignedRequest result = FacebookSignedRequest.Parse(app.InnerCurrent, request);
if (String.IsNullOrWhiteSpace(accessToken))
{
accessToken = result.AccessToken;
}
dynamic data = result.Data;
bool liked = data.page.liked;
//bool liked = true;
if (!liked)
{
Home h = Home.NotLiked();
return View(h);
}
else
{
Home h = Home.Liked();
FacebookWebClient fb = null;
if (String.IsNullOrWhiteSpace(accessToken))
{
var fbRequest = FacebookWebContext.Current;
if (fbRequest.IsAuthorized())
fb = new FacebookWebClient(fbRequest);
accessToken = fbRequest.AccessToken;
}
else
{
fb = new FacebookWebClient(accessToken);
}
if (fb != null)
{
dynamic r = fb.Get("/me");
//h.TestString2 += " Ha! We captured this data about you!";
//h.TestString2 += " Name: " + r.name;
//h.TestString2 += " Location: " + r.location;
//h.TestString2 += " Birthday: " + r.birthday;
//h.TestString2 += " About Me: " + r.aboutme;
//h.ImgUrl = "http://graph.facebook.com/" + r.id + "/picture?type=large";
//string fqlResult = "";
//var fbApp = new FacebookClient(accessToken);
//basic fql query execution
//dynamic friends = fbApp.Query("SELECT uid FROM page_admin WHERE page_id='160828267335555'");
//SELECT uid FROM page_fan WHERE page_id = 160828267335555
//"SELECT uid FROM page_fan WHERE uid=me() AND page_id=<your page id>";
//loop through all friends and get their name and create response containing friends' name and profile picture
//foreach (dynamic friend in friends)
//{
// fqlResult += friend.uid + ".... ";
// //fqlResult += friend.name + "<img src='" + friend.pic_square + "' alt='" + friend.name + "' /><br />";
//}
h.AddUser(r.id, accessToken, r.first_name, r.last_name, DateTime.ParseExact(r.birthday, "MM/dd/yyyy", new System.Globalization.CultureInfo("en-GB")), r.email, DateTime.Now, r.gender, "http://graph.facebook.com/" + r.id + "/picture?type=large");
//ViewBag.Likes = fqlResult;
}
else
{
//Display a message saying not authed....
}
// if statement to stop same user from being added to the database...
if ()
{
//condition here
}
else
{
//condition here
}
return View(h);
}
}
}
}
我现在正在尝试使用 if else if 和 else 语句。
var User = SELECT from User WHERE FBID = FBID();
if (User.Count==0)
{
User.AddUser(facebookID as string, accessToken as string, fName as string, lName as string, dob as DateTime, email as string, dateLiked as DateTime, gender as string, imageURL as string); //Create newUser
}
else if (User.Count ==1)
{
//set properties to User[0]
}
else
{
// THROW EXCEPTION
}
这些是我创建的数据库的属性;
//public string UserID { get; set; }
public string FBID { get; set; }
public string AccessToken { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string Gender { get; set; }
public DateTime DOB { get; set; }
public string Email { get; set; }
public DateTime DateLiked { get; set; }
public string ImageURL { get; set; }
//public string TestString { get; set; }
//public string TestString2 { get; set; }
public bool IsLiked { get; set; }
//public string ImgUrl { get; set; }
public void AddUser (string facebookID, string accessToken, string fName, string lName, DateTime dob, string email, DateTime dateLiked, string gender, string imageURL)
{
//UserID = userID;
FBID = facebookID;
AccessToken = accessToken;
FName = fName;
LName = lName;
DOB = dob;
Email = email;
DateLiked = dateLiked;
Gender = gender;
ImageURL = imageURL;
User newUser = new User();
Entities newContext = new Entities();
//newUser.UserID = 1;
newUser.FacebookID = facebookID;
newUser.AccessToken = accessToken;
newUser.FName = fName;
newUser.LName = lName;
newUser.Gender = gender;
newUser.DOB = DOB;
newUser.Email = email;
newUser.DateLiked = dateLiked;
newUser.ImageURL = imageURL;
newContext.Users.AddObject(newUser);
newContext.SaveChanges();
}
}
}
我希望这是有道理的,这里的新手,所以对我要轻松一些。 =)
I want to prevent the same user from being added to the database automatically. I want to check this against the FacebookID
field/column set in my database. How would I do this? Below is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCFacebookTestApp.Models;
using Facebook;
using Facebook.Web;
namespace MVCFacebookTestApp.Controllers
{
public class HomeController : Controller
{
public FacebookSession FacebookSession
{
get { return (FacebookWebContext.Current.Session); }
}
public ActionResult Index()
{
string request = Request.Form["signed_request"];
string accessToken = "";
if (Request.Form["access_token"] != null)
{
accessToken = Request.Form["access_token"];
}
FacebookApplication app = new FacebookApplication();
FacebookSignedRequest result = FacebookSignedRequest.Parse(app.InnerCurrent, request);
if (String.IsNullOrWhiteSpace(accessToken))
{
accessToken = result.AccessToken;
}
dynamic data = result.Data;
bool liked = data.page.liked;
//bool liked = true;
if (!liked)
{
Home h = Home.NotLiked();
return View(h);
}
else
{
Home h = Home.Liked();
FacebookWebClient fb = null;
if (String.IsNullOrWhiteSpace(accessToken))
{
var fbRequest = FacebookWebContext.Current;
if (fbRequest.IsAuthorized())
fb = new FacebookWebClient(fbRequest);
accessToken = fbRequest.AccessToken;
}
else
{
fb = new FacebookWebClient(accessToken);
}
if (fb != null)
{
dynamic r = fb.Get("/me");
//h.TestString2 += " Ha! We captured this data about you!";
//h.TestString2 += " Name: " + r.name;
//h.TestString2 += " Location: " + r.location;
//h.TestString2 += " Birthday: " + r.birthday;
//h.TestString2 += " About Me: " + r.aboutme;
//h.ImgUrl = "http://graph.facebook.com/" + r.id + "/picture?type=large";
//string fqlResult = "";
//var fbApp = new FacebookClient(accessToken);
//basic fql query execution
//dynamic friends = fbApp.Query("SELECT uid FROM page_admin WHERE page_id='160828267335555'");
//SELECT uid FROM page_fan WHERE page_id = 160828267335555
//"SELECT uid FROM page_fan WHERE uid=me() AND page_id=<your page id>";
//loop through all friends and get their name and create response containing friends' name and profile picture
//foreach (dynamic friend in friends)
//{
// fqlResult += friend.uid + ".... ";
// //fqlResult += friend.name + "<img src='" + friend.pic_square + "' alt='" + friend.name + "' /><br />";
//}
h.AddUser(r.id, accessToken, r.first_name, r.last_name, DateTime.ParseExact(r.birthday, "MM/dd/yyyy", new System.Globalization.CultureInfo("en-GB")), r.email, DateTime.Now, r.gender, "http://graph.facebook.com/" + r.id + "/picture?type=large");
//ViewBag.Likes = fqlResult;
}
else
{
//Display a message saying not authed....
}
// if statement to stop same user from being added to the database...
if ()
{
//condition here
}
else
{
//condition here
}
return View(h);
}
}
}
}
I'm trying it this way at the moment using the if else if and else statement.
var User = SELECT from User WHERE FBID = FBID();
if (User.Count==0)
{
User.AddUser(facebookID as string, accessToken as string, fName as string, lName as string, dob as DateTime, email as string, dateLiked as DateTime, gender as string, imageURL as string); //Create newUser
}
else if (User.Count ==1)
{
//set properties to User[0]
}
else
{
// THROW EXCEPTION
}
and these are my properties to the database that i have created;
//public string UserID { get; set; }
public string FBID { get; set; }
public string AccessToken { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string Gender { get; set; }
public DateTime DOB { get; set; }
public string Email { get; set; }
public DateTime DateLiked { get; set; }
public string ImageURL { get; set; }
//public string TestString { get; set; }
//public string TestString2 { get; set; }
public bool IsLiked { get; set; }
//public string ImgUrl { get; set; }
public void AddUser (string facebookID, string accessToken, string fName, string lName, DateTime dob, string email, DateTime dateLiked, string gender, string imageURL)
{
//UserID = userID;
FBID = facebookID;
AccessToken = accessToken;
FName = fName;
LName = lName;
DOB = dob;
Email = email;
DateLiked = dateLiked;
Gender = gender;
ImageURL = imageURL;
User newUser = new User();
Entities newContext = new Entities();
//newUser.UserID = 1;
newUser.FacebookID = facebookID;
newUser.AccessToken = accessToken;
newUser.FName = fName;
newUser.LName = lName;
newUser.Gender = gender;
newUser.DOB = DOB;
newUser.Email = email;
newUser.DateLiked = dateLiked;
newUser.ImageURL = imageURL;
newContext.Users.AddObject(newUser);
newContext.SaveChanges();
}
}
}
I hope it makes sense, newbie here so go easy on me. =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了保持数据库的完整性,您应该向数据库中的此列添加唯一约束,然后 执行 UPDATE/INSERT 时捕获违反此约束的异常 询问。
To preserve the integrity of your database you should add a unique constraint to this column in your database and then catch the exception of the violation of this constraint when performing the UPDATE/INSERT query.