如何实现“注销”功能 使用 ASP.NET MVC 链接?
这似乎是一个非常愚蠢的问题,但我正在尝试找出最好的方法来做到这一点。 您是否会简单地重定向到 /Logout 页面并让控制器调用 FormsAuthentication.SignOut 函数?
这是我的第一个想法,但后来我想知道它是否会被第三方网站滥用。 假设有人刚刚决定发布一个指向您的 /Logout 页面的链接。 用户将从您的应用程序中注销。 有没有好的办法来防止呢?
This seems like a pretty stupid question, but I'm trying to figure out the best way to do this. Would you simply redirect to a /Logout page and have the controller call the FormsAuthentication.SignOut function?
That was my first thought, but then I wondered if it could be abused by third party websites. Let's say someone just decides to post a link to your /Logout page. The user would get signed out of your application. Is there a good way to prevent that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
新的 ASP.net MVC Beta 包含一个 AccountController,它可能值得一看,因为它本质上实现了从注册到登录/注销再到忘记密码功能的所有功能。 不确定它有多好,但肯定是一个很好的起点。
The new ASP.net MVC Beta contains an AccountController, which may be worth looking at, as it essentially implements everything from Registration to Login/Logout to Forgot Password functionality. Not sure how good it is, but a good starting Point for sure.
从 ActionResult 派生
Derive from ActionResult
您应该寻找 cookie 或其他将客户端识别为真正用户的东西。
You should look for a cookie or something that identifies the client as the true user.
如果您担心用户通过使用恶意链接意外注销您的应用程序,您可以检查引用者以确保注销来自您的网站(或者在用户简单键入的情况下为 NULL)中的网址)。
实际上我并不担心这一点,因为注销某人很烦人,但不一定存在安全风险。
If you are concerned about a user getting accidentally logged out of you application through the use of a malicious link, you can check the Referrer to make sure that the logout is coming from your site (or is NULL in the case where the user simply types the URL in).
I actually don't worry about this since logging someone out is annoying but not necessarily a security risk.
此类恶意链接是一类安全漏洞的示例,称为跨站点请求伪造 (CSRF)。 注销链接相对无害,但远程站点可以设置许多隐藏表单并将它们发布到您的站点,以通过 POST 执行任何可能的操作。
最常见的对策是在每个表单中包含一个挑战、一个随机隐藏值,然后检查该值。 检查引用标头可能有效,但请注意,某些浏览器根本不发送引用。
了解更多:http://en.wikipedia.org/wiki/Cross-site_request_forgery
Such a malicious link would be an example of a class of security vulnerabilities known as cross site request forgery, CSRF. A logout link is relatively harmless, but a remote site could set up a number of hidden forms and post them to your site to perform any action possible through POST.
The most common counter-measure is to include a challenge, a random hidden value in each form, and then check for that value. Checking the referer header could work, but note that some browsers don't send referer at all.
Read more: http://en.wikipedia.org/wiki/Cross-site_request_forgery
这是一个老问题,但这里有一个 MVC 的现代示例:
您可以确保
Logout
操作只能由通过应用Authorize
登录的人调用code> 属性给它。This is an old question, but here is a modern example with MVC:
You can ensure that the
Logout
action is only able to be called by somebody who is logged in by applying theAuthorize
attribute to it.这就是我用的。
似乎工作正常。
第三方网站只会自行退出。 因此,他们不会获得与实际单击“注销”不同的任何效果。
This is what I use.
Seems to work fine.
Third party websites are only going to log themselves out. So they wouldn't be achieving anything different from actually clicking Logout.