Asp.net 使用MailChimp WebHooks功能

Asp.net 使用MailChimp WebHooks功能,asp.net,mailchimp,webhooks,Asp.net,Mailchimp,Webhooks,有人可以提供在ASP.NET中执行此操作的示例吗。我们想做一些MailCHimp–内部数据库同步,并计划使用webhooks功能来完成,但我们就是无法让它工作。当有人从mail chimp退订时,我们希望使用web钩子同步数据 另一个需要解决的问题是安全性。我们如何保护此页面不被恶意用户访问?以下是一段适用于我们的代码。这是相当简单的,但我们确实花了一些实验来让它工作 if (Request.Form["type"] != null && Request.Form

有人可以提供在ASP.NET中执行此操作的示例吗。我们想做一些MailCHimp–内部数据库同步,并计划使用webhooks功能来完成,但我们就是无法让它工作。当有人从mail chimp退订时,我们希望使用web钩子同步数据


另一个需要解决的问题是安全性。我们如何保护此页面不被恶意用户访问?

以下是一段适用于我们的代码。这是相当简单的,但我们确实花了一些实验来让它工作

        if (Request.Form["type"] != null && Request.Form["type"] == "unsubscribe")
        {

            string email = Request.Form["data[merges][EMAIL]"];
           //now you can do insert/update data in your local database
     }
查看API文档了解更多详细信息


关于安全问题,你可以做很多事情,但这取决于你想去多深。我建议的一件事是检查您的IIS日志,找出mail chimp使用哪个IP地址/用户代理来触发web钩子,然后仅为除此之外的所有其他IP地址阻止此页面。您可能还可以做其他一些事情来确保这一点,比如使用不容易猜到的页面名f3jijselife.aspx远比webhooks好。aspx

下面是一段适用于我们的代码。这是相当简单的,但我们确实花了一些实验来让它工作

        if (Request.Form["type"] != null && Request.Form["type"] == "unsubscribe")
        {

            string email = Request.Form["data[merges][EMAIL]"];
           //now you can do insert/update data in your local database
     }
查看API文档了解更多详细信息


关于安全问题,你可以做很多事情,但这取决于你想去多深。我建议的一件事是检查您的IIS日志,找出mail chimp使用哪个IP地址/用户代理来触发web钩子,然后仅为除此之外的所有其他IP地址阻止此页面。您可能还可以做其他一些事情来确保这一点,比如使用不容易猜到的页面名f3jijselife.aspx远比webhooks好。aspx

我最近刚刚根据他们提供的PHP代码实现了这一点,这里是框架。。。我拿出了实际的实现,但希望会有用

public class MailChimpWebHook : IHttpHandler
{
    private static readonly ILog Logger = LogManager.GetLogger(typeof(MailChimpWebHook));

    private const string Key = "xxxx";
    private const string ParamKey = "key";
    private const string ParamType = "type";
    private const string ParamListId = "data[list_id]";
    private const string ParamListIdNew = "data[new_id]";
    private const string ParamEmail = "data[email]";
    private const string ParamOldEmail = "data[new_email]";
    private const string ParamNewEmail = "data[old_email]";
    private const string ParamProfileEmail = "data[merges][EMAIL]";
    private const string ParamProfileFirstName = "data[merges][FNAME]";
    private const string ParamProfileLastName = "data[merges][LNAME]";
    private const string ParamProfileGroups = "data[merges][INTERESTS]";

    private const string TypeSubscribe = "subscribe";
    private const string TypeUnsubscribe = "unsubscribe";
    private const string TypeCleaned = "cleaned";
    private const string TypeUpdateEmail = "upemail";
    private const string TypeProfileUpdate = "profile";

    public void ProcessRequest(HttpContext context)
    {
        Logger.Info("==================[ Incoming Request ]==================");

        if (string.IsNullOrEmpty(context.Request[ParamKey]))
        {
            Logger.Warn("No security key specified, ignoring request"); 
        }
        else if (context.Request[ParamKey] != Key)
        {
            Logger.WarnFormat("Security key specified, but not correct. Wanted: '{0}' | , but received '{1}'", Key, context.Request[ParamKey]);
        }
        else
        {
             //process the request
            Logger.InfoFormat("Processing a '{0}' request...", context.Request[ParamType]);

            try
            {
                switch (context.Request[ParamType])
                {
                    case TypeSubscribe:
                        Subscribe(context.Request);
                        break;
                    case TypeUnsubscribe:
                        Unsubscribe(context.Request);
                        break;
                    case TypeCleaned:
                        Cleaned(context.Request);
                        break;
                    case TypeUpdateEmail:
                        UpdateEmail(context.Request);
                        break;
                    case TypeProfileUpdate:
                        UpdateProfile(context.Request);
                        break;
                    default:
                        Logger.WarnFormat("Request type '{0}' unknown, ignoring.", context.Request[ParamType]);
                        break;
                }
            }
            catch (Exception e)
            {
                Logger.Error("There was an error processing the callback", e);
            }
        }

        Logger.Info("Finished processing request.");
    }

    private void UpdateProfile(HttpRequest httpRequest)
    {
        Logger.Info("Processing update profile request!");

        #region [ sample data structure ]
        //  "type": "profile", 
        //  "fired_at": "2009-03-26 21:31:21", 
        //  "data[id]": "8a25ff1d98", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[email]": "api@mailchimp.com", 
        //  "data[email_type]": "html", 
        //  "data[merges][EMAIL]": "api@mailchimp.com", 
        //  "data[merges][FNAME]": "MailChimp", 
        //  "data[merges][LNAME]": "API", 
        //  "data[merges][INTERESTS]": "Group1,Group2", 
        //  "data[ip_opt]": "10.20.10.30"
        #endregion
    }

    private void UpdateEmail(HttpRequest httpRequest)
    {
        Logger.Info("Processing update email request!");

        #region [ sample data structure ]
        //  "type": "upemail", 
        //  "fired_at": "2009-03-26\ 22:15:09", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[new_id]": "51da8c3259", 
        //  "data[new_email]": "api+new@mailchimp.com", 
        //  "data[old_email]": "api+old@mailchimp.com"
        #endregion

    }

    private void Cleaned(HttpRequest httpRequest)
    {
        Logger.Info("Processing cleaned email request!");

        #region [ sample data structure ]
        //  "type": "cleaned", 
        //  "fired_at": "2009-03-26 22:01:00", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[campaign_id]": "4fjk2ma9xd",
        //  "data[reason]": "hard",
        //  "data[email]": "api+cleaned@mailchimp.com"
        #endregion
    }

    private void Unsubscribe(HttpRequest httpRequest)
    {
        Logger.Info("Processing unsubscribe...");

        #region [ sample data structure ]
        //  "type": "unsubscribe", 
        //  "fired_at": "2009-03-26 21:40:57",  
        //  "data[action]": "unsub",
        //  "data[reason]": "manual", 
        //  "data[id]": "8a25ff1d98", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[email]": "api+unsub@mailchimp.com", 
        //  "data[email_type]": "html", 
        //  "data[merges][EMAIL]": "api+unsub@mailchimp.com", 
        //  "data[merges][FNAME]": "MailChimp", 
        //  "data[merges][LNAME]": "API", 
        //  "data[merges][INTERESTS]": "Group1,Group2", 
        //  "data[ip_opt]": "10.20.10.30",
        //  "data[campaign_id]": "cb398d21d2",
        //  "data[reason]": "hard"
        #endregion

    }

    private void Subscribe(HttpRequest httpRequest)
    {
        Logger.Info("Processing subscribe...");

        #region [ sample data structure ]
        //  "type": "subscribe", 
        //  "fired_at": "2009-03-26 21:35:57", 
        //  "data[id]": "8a25ff1d98", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[email]": "api@mailchimp.com", 
        //  "data[email_type]": "html", 
        //  "data[merges][EMAIL]": "api@mailchimp.com", 
        //  "data[merges][FNAME]": "MailChimp", 
        //  "data[merges][LNAME]": "API", 
        //  "data[merges][INTERESTS]": "Group1,Group2", 
        //  "data[ip_opt]": "10.20.10.30", 
        //  "data[ip_signup]": "10.20.10.30"
        #endregion

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

我最近刚刚根据他们提供的PHP代码实现了这个框架。。。我拿出了实际的实现,但希望会有用

public class MailChimpWebHook : IHttpHandler
{
    private static readonly ILog Logger = LogManager.GetLogger(typeof(MailChimpWebHook));

    private const string Key = "xxxx";
    private const string ParamKey = "key";
    private const string ParamType = "type";
    private const string ParamListId = "data[list_id]";
    private const string ParamListIdNew = "data[new_id]";
    private const string ParamEmail = "data[email]";
    private const string ParamOldEmail = "data[new_email]";
    private const string ParamNewEmail = "data[old_email]";
    private const string ParamProfileEmail = "data[merges][EMAIL]";
    private const string ParamProfileFirstName = "data[merges][FNAME]";
    private const string ParamProfileLastName = "data[merges][LNAME]";
    private const string ParamProfileGroups = "data[merges][INTERESTS]";

    private const string TypeSubscribe = "subscribe";
    private const string TypeUnsubscribe = "unsubscribe";
    private const string TypeCleaned = "cleaned";
    private const string TypeUpdateEmail = "upemail";
    private const string TypeProfileUpdate = "profile";

    public void ProcessRequest(HttpContext context)
    {
        Logger.Info("==================[ Incoming Request ]==================");

        if (string.IsNullOrEmpty(context.Request[ParamKey]))
        {
            Logger.Warn("No security key specified, ignoring request"); 
        }
        else if (context.Request[ParamKey] != Key)
        {
            Logger.WarnFormat("Security key specified, but not correct. Wanted: '{0}' | , but received '{1}'", Key, context.Request[ParamKey]);
        }
        else
        {
             //process the request
            Logger.InfoFormat("Processing a '{0}' request...", context.Request[ParamType]);

            try
            {
                switch (context.Request[ParamType])
                {
                    case TypeSubscribe:
                        Subscribe(context.Request);
                        break;
                    case TypeUnsubscribe:
                        Unsubscribe(context.Request);
                        break;
                    case TypeCleaned:
                        Cleaned(context.Request);
                        break;
                    case TypeUpdateEmail:
                        UpdateEmail(context.Request);
                        break;
                    case TypeProfileUpdate:
                        UpdateProfile(context.Request);
                        break;
                    default:
                        Logger.WarnFormat("Request type '{0}' unknown, ignoring.", context.Request[ParamType]);
                        break;
                }
            }
            catch (Exception e)
            {
                Logger.Error("There was an error processing the callback", e);
            }
        }

        Logger.Info("Finished processing request.");
    }

    private void UpdateProfile(HttpRequest httpRequest)
    {
        Logger.Info("Processing update profile request!");

        #region [ sample data structure ]
        //  "type": "profile", 
        //  "fired_at": "2009-03-26 21:31:21", 
        //  "data[id]": "8a25ff1d98", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[email]": "api@mailchimp.com", 
        //  "data[email_type]": "html", 
        //  "data[merges][EMAIL]": "api@mailchimp.com", 
        //  "data[merges][FNAME]": "MailChimp", 
        //  "data[merges][LNAME]": "API", 
        //  "data[merges][INTERESTS]": "Group1,Group2", 
        //  "data[ip_opt]": "10.20.10.30"
        #endregion
    }

    private void UpdateEmail(HttpRequest httpRequest)
    {
        Logger.Info("Processing update email request!");

        #region [ sample data structure ]
        //  "type": "upemail", 
        //  "fired_at": "2009-03-26\ 22:15:09", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[new_id]": "51da8c3259", 
        //  "data[new_email]": "api+new@mailchimp.com", 
        //  "data[old_email]": "api+old@mailchimp.com"
        #endregion

    }

    private void Cleaned(HttpRequest httpRequest)
    {
        Logger.Info("Processing cleaned email request!");

        #region [ sample data structure ]
        //  "type": "cleaned", 
        //  "fired_at": "2009-03-26 22:01:00", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[campaign_id]": "4fjk2ma9xd",
        //  "data[reason]": "hard",
        //  "data[email]": "api+cleaned@mailchimp.com"
        #endregion
    }

    private void Unsubscribe(HttpRequest httpRequest)
    {
        Logger.Info("Processing unsubscribe...");

        #region [ sample data structure ]
        //  "type": "unsubscribe", 
        //  "fired_at": "2009-03-26 21:40:57",  
        //  "data[action]": "unsub",
        //  "data[reason]": "manual", 
        //  "data[id]": "8a25ff1d98", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[email]": "api+unsub@mailchimp.com", 
        //  "data[email_type]": "html", 
        //  "data[merges][EMAIL]": "api+unsub@mailchimp.com", 
        //  "data[merges][FNAME]": "MailChimp", 
        //  "data[merges][LNAME]": "API", 
        //  "data[merges][INTERESTS]": "Group1,Group2", 
        //  "data[ip_opt]": "10.20.10.30",
        //  "data[campaign_id]": "cb398d21d2",
        //  "data[reason]": "hard"
        #endregion

    }

    private void Subscribe(HttpRequest httpRequest)
    {
        Logger.Info("Processing subscribe...");

        #region [ sample data structure ]
        //  "type": "subscribe", 
        //  "fired_at": "2009-03-26 21:35:57", 
        //  "data[id]": "8a25ff1d98", 
        //  "data[list_id]": "a6b5da1054",
        //  "data[email]": "api@mailchimp.com", 
        //  "data[email_type]": "html", 
        //  "data[merges][EMAIL]": "api@mailchimp.com", 
        //  "data[merges][FNAME]": "MailChimp", 
        //  "data[merges][LNAME]": "API", 
        //  "data[merges][INTERESTS]": "Group1,Group2", 
        //  "data[ip_opt]": "10.20.10.30", 
        //  "data[ip_signup]": "10.20.10.30"
        #endregion

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

我使用的是C WebAPI,我的解决方案是使用webhook发送的POST-MailChimp正文中的FormDataCollection对象

    using System.Net.Http.Formatting;

    [HttpPost]
    [Route("mailchimp/subscriber")]
    public IHttpActionResult Post([FromBody] FormDataCollection data)
    {
        if (data != null)
        {
            string type = data.Get("type");

            if (!string.IsNullOrWhiteSpace(type))
            {
                string listId = data.Get("data[list_id]");
                string id = data.Get("data[id]");
                string firstName = data.Get("data[merges][FNAME]");
                string lastName = data.Get("data[merges][LNAME]");
                string email = data.Get("data[email]");

                if (!string.IsNullOrWhiteSpace(email))
                {
                    // Do something with the subscriber
                }
            }
        }
    }

我使用的是C WebAPI,我的解决方案是使用webhook发送的POST-MailChimp正文中的FormDataCollection对象

    using System.Net.Http.Formatting;

    [HttpPost]
    [Route("mailchimp/subscriber")]
    public IHttpActionResult Post([FromBody] FormDataCollection data)
    {
        if (data != null)
        {
            string type = data.Get("type");

            if (!string.IsNullOrWhiteSpace(type))
            {
                string listId = data.Get("data[list_id]");
                string id = data.Get("data[id]");
                string firstName = data.Get("data[merges][FNAME]");
                string lastName = data.Get("data[merges][LNAME]");
                string email = data.Get("data[email]");

                if (!string.IsNullOrWhiteSpace(email))
                {
                    // Do something with the subscriber
                }
            }
        }
    }

我完全支持詹姆斯的答案

然而,当我自己尝试实现webhook时,我发现您还需要实现GET方法,以便能够在MailChimp中创建webhook

这对我来说是个好办法:

[HttpGet]
[HttpOptions]
public HttpResponseMessage Get()
{
    return Request.CreateResponse(HttpStatusCode.OK);
}
MailChimp文档:
我完全支持詹姆斯的回答

然而,当我自己尝试实现webhook时,我发现您还需要实现GET方法,以便能够在MailChimp中创建webhook

这对我来说是个好办法:

[HttpGet]
[HttpOptions]
public HttpResponseMessage Get()
{
    return Request.CreateResponse(HttpStatusCode.OK);
}
MailChimp文档: