在 C# 中存储和检索 cookie 值
我有一个老虎机应用程序,它使用 C# 显示 Flash 文件。任何用户都会访问该网站并玩游戏。但我需要限制每天最多3次,这意味着他只能玩这个游戏3次,如果他第四次去玩,那么他会收到一条消息,表明他第二天可以玩。由于此站点上没有用户登录,因此我决定存储 cookie 值。每次他进入该站点时,计数器都会增加。然后,在每个 page_load 事件中,检索 cookie 值,如果其值为 3,则显示“禁止播放”消息。
首先,我将检索页面加载事件中 cookie 的值。
string _counter = response.Cookies["slotmachinecookie"]["counter"];
if (!IsPostBack)
{
if (_counter) = 3
//displayMessage
return;
else
{
HttpCookie cookie = new HttpCookie("slotmachinecookie");
if (string.IsNullOrEmpty(_counter))
cookie.Values.Add("1", "counter");
else if (_counter == "1")
cookie.Values.Add("2", "counter")
else if (_counter == "2")
cookie.Values.Add("3", "counter");
}
}
这行得通吗?我想我可以尝试一下,但只是想确定除了cookie之外是否还有更好的方法来做到这一点?提前致谢。
I have a slot machine application which displays a flash file using c#. Any user would come to the site and play the game. But I would need to limit that a max of 3 times per day, meaning he could play the game only 3 times and if he goes to play the fourth time, then he would get a message indicating he could play the next day. Since there is no user login on this site, i have decided to store cookie values. It would be counter that would increment each time he gets into the site. Then, on each page_load event, retrieve the cookie value and if its 3, then display the No Play Message.
First i would retrieve the value of cookie on page load event.
string _counter = response.Cookies["slotmachinecookie"]["counter"];
if (!IsPostBack)
{
if (_counter) = 3
//displayMessage
return;
else
{
HttpCookie cookie = new HttpCookie("slotmachinecookie");
if (string.IsNullOrEmpty(_counter))
cookie.Values.Add("1", "counter");
else if (_counter == "1")
cookie.Values.Add("2", "counter")
else if (_counter == "2")
cookie.Values.Add("3", "counter");
}
}
Will this work? I guess i could try it but just want to make sure if there is a better way other than cookies to do this? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的评论,您的方法会起作用,但精明的用户可以通过清除 cookie 来克服您的使用限制并继续整天玩。
虽然 cookie 方法很简单,但它确实有一个漏洞。
您可能需要考虑的是捕获用户的传入 IP 地址并将其存储在数据库、本地缓存(取决于应用程序的架构方式)或其他存储库中。
您可以使用服务器请求变量
REMOTE_ADDR
捕获传入的 IP 地址。同样,根据您的应用程序的托管方式,您的提供商可能无法正确报告此情况(例如,如果您的服务器位于负载均衡器后面,则可能是负载均衡器的 IP)。您可能还想检查您的提供商是否使用“X_FORWARDED_FOR”标头转发此信息。 (有些设备使用此标头名称的不同变体...请检查您的文档。)
因此,这会稍微复杂一些,但也能够进一步弥补该漏洞,将使用限制为每天仅 3 次。
我希望这有帮助。祝你好运!
Based on my comment, your approach will work, but a savvy user could outwit your usage limitation by clearing cookies and continue playing all day long.
While the cookie approach is easy, it does have a hole in it.
What you may want to consider is capturing the user's incoming IP address and storing it in a database, or local cache (depending on how your application is architected), or some other repository.
You can capture the incoming IP address by using the server request variable
REMOTE_ADDR
.Again, based on how your app is hosted, your provider may not report this correctly (e.g. if your server is behind a load balancer, which may be the IP of the load balancer). You may also want to check if your provider forwards this information using a "X_FORWARDED_FOR" header. (Some devices use different variations on the name of this header...check your documentation.)
So this will be a little more complex, but also will be able to close that hole a bit more to limit usage to only 3 times a day.
I hope this helps. Good luck!
我没有看到您在哪里检查日期并将计数重置为零(如果是新的一天)。
而且,是每天还是每 24 小时?
I didn't see where you are checking the date and resetting the count to zero if it's a new day.
And, is it per day or per 24 hour period?