如何实现“注销”功能 使用 ASP.NET MVC 链接?

发布于 2024-07-07 18:58:49 字数 212 浏览 6 评论 0原文

这似乎是一个非常愚蠢的问题,但我正在尝试找出最好的方法来做到这一点。 您是否会简单地重定向到 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

︶ ̄淡然 2024-07-14 18:58:50

新的 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.

提笔书几行 2024-07-14 18:58:50

从 ActionResult 派生

public class LogoutResult : ActionResult
{
    private readonly IAuthenticationService _authenticationService;
    private readonly IWebContext _context;

    public LogoutResult(IAuthenticationService authenticationService, IWebContext context)
    {
        _authenticationService = authenticationService;
        _context = context;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        _authenticationService.Logout();
        _context.Abandon();
        _context.Redirect("~/");
    }
}

Derive from ActionResult

public class LogoutResult : ActionResult
{
    private readonly IAuthenticationService _authenticationService;
    private readonly IWebContext _context;

    public LogoutResult(IAuthenticationService authenticationService, IWebContext context)
    {
        _authenticationService = authenticationService;
        _context = context;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        _authenticationService.Logout();
        _context.Abandon();
        _context.Redirect("~/");
    }
}
痴情 2024-07-14 18:58:50

您应该寻找 cookie 或其他将客户端识别为真正用户的东西。

You should look for a cookie or something that identifies the client as the true user.

烂人 2024-07-14 18:58:49

如果您担心用户通过使用恶意链接意外注销您的应用程序,您可以检查引用者以确保注销来自您的网站(或者在用户简单键入的情况下为 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.

夏有森光若流苏 2024-07-14 18:58:49

此类恶意链接是一类安全漏洞的示例,称为跨站点请求伪造 (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

回首观望 2024-07-14 18:58:49

这是一个老问题,但这里有一个 MVC 的现代示例:

[Authorize]
public RedirectResult Logout()
{
    FormsAuthentication.SignOut();

    return this.Redirect("/");
}

您可以确保 Logout 操作只能由通过应用 Authorize 登录的人调用code> 属性给它。

This is an old question, but here is a modern example with MVC:

[Authorize]
public RedirectResult Logout()
{
    FormsAuthentication.SignOut();

    return this.Redirect("/");
}

You can ensure that the Logout action is only able to be called by somebody who is logged in by applying the Authorize attribute to it.

鹤舞 2024-07-14 18:58:49

这就是我用的。

public ActionResult Logout()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

似乎工作正常。

第三方网站只会自行退出。 因此,他们不会获得与实际单击“注销”不同的任何效果。

This is what I use.

public ActionResult Logout()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文