有人可以請提供在ASP.NET中執行此操作的示例。我們想要做一些MailCHimp - 內部數據庫同步,並計劃使用webhooks功能來做到這一點,但我們無法完成工作。當有人從郵件黑猩猩取消訂閱時,我們希望使用網頁鉤子來同步數據。使用MailChimp WebHooks功能
要解決的另一件事是安全性。我們如何確保此頁面不被惡意用戶訪問?
有人可以請提供在ASP.NET中執行此操作的示例。我們想要做一些MailCHimp - 內部數據庫同步,並計劃使用webhooks功能來做到這一點,但我們無法完成工作。當有人從郵件黑猩猩取消訂閱時,我們希望使用網頁鉤子來同步數據。使用MailChimp WebHooks功能
要解決的另一件事是安全性。我們如何確保此頁面不被惡意用戶訪問?
這是一段適合我們的代碼。這很簡單,但它確實需要我們進行一些試驗才能使其發揮作用。
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文檔瞭解更多信息http://apidocs.mailchimp.com/webhooks/
在安全方面,你可以做一噸的東西,但它取決於你想有多深去。我建議的一件事是檢查你的IIS日誌,並找到郵件黑猩猩使用哪個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]": "[email protected]",
// "data[email_type]": "html",
// "data[merges][EMAIL]": "[email protected]",
// "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]": "[email protected]",
// "data[old_email]": "[email protected]"
#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]": "[email protected]"
#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]": "[email protected]",
// "data[email_type]": "html",
// "data[merges][EMAIL]": "[email protected]",
// "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]": "[email protected]",
// "data[email_type]": "html",
// "data[merges][EMAIL]": "[email protected]",
// "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,對我的解決方案是有用的是使用FormDataCollection對象從POST MailChimp的身體與發送網絡掛接。
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
}
}
}
}