C# 在上/下投票系统中使用cookie
我想为从数据库检索到的几篇文章建立上/下投票系统,但我想为每篇文章添加cookie以限制投票数,这样cookie将在一天内过期,但我不知道在哪里添加适当的代码 更多详情:C# 在上/下投票系统中使用cookie,c#,asp.net,voting-system,updown,C#,Asp.net,Voting System,Updown,我想为从数据库检索到的几篇文章建立上/下投票系统,但我想为每篇文章添加cookie以限制投票数,这样cookie将在一天内过期,但我不知道在哪里添加适当的代码 更多详情: <script> function vote(id, value) { // function vote with 2 arguments: article ID and value (+1 or -1) depending if you clicked on the arrow up or down
<script>
function vote(id, value) { // function vote with 2 arguments: article ID and value (+1 or -1) depending if you clicked on the arrow up or down
var dataFields = { 'id': id, 'value': value }; // We pass the 2 arguments
$.ajax({ // Ajax
type: "POST",
dataType: "text",//This for indicate that you'r expecting a text response from server
url: "WebService.asmx/updateVotes",
data: dataFields,
timeout: 3000,
success: function (dataBack) {
if(
$('#number' + id).html(dataBack);// div "number" with the new number
$('#arrow_up' + id).html('<div class="arrow_up_voted"></div>'); // We replace the clickable "arrow up" by the not clickable one
$('#arrow_down' + id).html('<div class="arrow_down_voted"></div>'); // We replace the clickable "arrow down" by the not clickable one
$('#message' + id).html('<div id="alertFadeOut' + id + '" style="color: green">Thank you for voting</div>'); // Diplay message with a fadeout
$('#alertFadeOut' + id).fadeOut(1100, function () {
$('#alertFadeOut' + id).text('');
});
},
error: function () {
$('#number' + id).text('Problem!');
}
});
}
</script>
这就是webmethod
现在,我试着大声思考,并编写了以下函数来检查cookie是否为null
public bool enableVoting()
{
HttpCookie cookie = Request.Cookies["enableVote"];
if (Request.Cookies["enableVote"] != null)
{
return true;
}
else
{
return false;
}
}
我知道这是错的,但至少我试过了
另外,在何处添加for each循环,以便在用户为文章投票时添加cookie
foreach(post p in db.posts){
HttpCookie cookie = new HttpCookie("enableVote"+p.ID);
cookie.Value = "article:"+p.ID;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
一个建议。不要用饼干来做这个。他们很容易被操纵。清除浏览器历史记录,您可以再次投票。。再一次。。一次又一次 相反,在数据库中创建一个投票表,并添加一条记录,其中包含投票人的ip和他们投票的帖子的id以及时间戳 通过这种方式,您可以轻松地计算选票,当有人投票时,您可以快速检查该IP上次为该文章(或任何文章)投票的时间 此外,通过数据库中的投票表,您可以轻松捕获对所有内容进行投票的机器人程序,并限制单个ip对任何文章的投票数(可能每几分钟不超过1或2次) 如果您担心同一IP后面的多个人无法投票,您可以将浏览器名称包括在内,并且只将唯一IP和浏览器版本计算为投票。这是相当独特的。它也可以被操纵,但对普通用户来说有点难 更新: 我使用这段代码的目的是在我的一个MVC项目中获得一个有点独特的键。用户可以切换浏览器并再次投票,但这需要一点努力,而且比清除浏览器历史记录更痛苦 我将IP、浏览器和国家组合成一个字符串,并将其用作投票键
public class UserInfo
{
public String ip { get; private set; }
public String browser { get; private set; }
public String country { get; private set; }
public UserInfo(HttpRequestBase Request)
{
ip = Request.UserHostAddress;
browser = Request.Browser.Platform + " " + Request.Browser.Type + "/" + Request.Browser.Id + " " + Request.Browser.Version;
country = "";
if (Request.UserLanguages.Length > 0)
country += " - " + Request.UserLanguages.ElementAt(0);
}
}
我在这里使用这个系统:检查用户是否有任何以前上传的文件(尝试上传一些东西并关闭浏览器,然后再去那里,它将在列表中)。谢谢你的建议,你有任何示例或链接来解释如何获取投票人的ip吗?使用Asp.net C#我可以这样做吗?
public class UserInfo
{
public String ip { get; private set; }
public String browser { get; private set; }
public String country { get; private set; }
public UserInfo(HttpRequestBase Request)
{
ip = Request.UserHostAddress;
browser = Request.Browser.Platform + " " + Request.Browser.Type + "/" + Request.Browser.Id + " " + Request.Browser.Version;
country = "";
if (Request.UserLanguages.Length > 0)
country += " - " + Request.UserLanguages.ElementAt(0);
}
}