简体   繁体   English

保护Web方法

[英]Securing a Web Method

Say I've got a method in c# MVC to send email with ajax, like: 假设我在c#MVC中有一个方法来发送带有ajax的电子邮件,例如:

public class mailController : Controller {

        SmtpClient mailserver = new SmtpClient("smtp.foo.com");

        public string send(string from, string to, string subject = "", string body = "", string cc = "", string bcc = "") {
            MailMessage message = new MailMessage(from, to, subject, body);
            if (cc.Length > 0) {
                message.CC.Add(cc);
            }
            if (bcc.Length > 0) {
                message.Bcc.Add(bcc);
            }
            mailserver.Send(message);
            return "MessageSent";
        }
    }

Is there anything I can do to make this more secure? 有什么办法可以让它更安全吗? I mean, as it stands anyone can type the relevant info into their address bar. 我的意思是,现在任何人都可以在地址栏中输入相关信息。 http://www.foo.com/mail/send?from=etc If I want to use this for form submission, I can't password protect it, or I'd have to use that in the javascript, which is easy to find. http://www.foo.com/mail/send?from=etc如果我想用它来表单提交,我无法用密码保护它,或者我必须在javascript中使用它,这很容易找到。 I considered setting a cookie and using that as authentication, but that only goes so far. 我考虑过设置一个cookie并将其用作身份验证,但这只是到目前为止。 Is there a standard procedure for protecting ajax methods? 是否有保护ajax方法的标准程序?

您需要在服务器上验证参数是否符合您的要求。

You need to implement a secure session token, to prevent unauthorized users (those without valid sessions) from being able to send an email. 您需要实现安全会话令牌,以防止未经授权的用户(没有有效会话的用户)发送电子邮件。 This is basically no different than any other cross site request forgery (CSRF) attack vector. 这与任何其他跨站点请求伪造(CSRF)攻击向量基本没有区别。 If you need any additional information just Google 'CSRF protection ASP.NET`' or similar to get some more concrete examples of how to do this. 如果您需要任何其他信息,只需使用Google的“CSRF保护ASP.NET”或类似内容,以获得更具体的示例,了解如何执行此操作。

Your requirements sound mutually exclusive. 您的要求听起来互相排斥。

If you do want to leave it public, but you don't want it abused, then maybe you could provide some sort of throttle where you only allow x number of requests from a specific IP address. 如果您确实希望将其公开,但您不希望它被滥用,那么也许您可以提供某种限制,只允许来自特定IP地址的x个请求。

You can also use mailto: in an HTMLform to prompt the client to send the email. 您还可以在HTMLform中使用mailto:来提示客户端发送电子邮件。

首先,您可以随时存储访问您的控制器的IP,如果相同的IP尝试以您定义的特定频率发送邮件,您可以阻止它阻止它...第二,您可以在邮件中生成随机数将发送给控制器的页面 - >这将允许您验证邮件是从您的站点发送的,而不是从第三方发送的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM