MVP 4でWebポストWebHook呼び出しを処理するWebhookハンドラを作成する。私はそれがすべて働いているが、今いくつかの認証を追加したい。mvcコントローラでWebHook呼び出しを認証するhttp:// secretkey:[email protected]
Easypostは、WebHookハンドラのURLにユーザ名または秘密鍵を含める方法を提案しています(https://username:[email protected]/easypost-webhook
reference)。それから私はその秘密鍵の値を抽出し、期待値と比較することを想定します。私は正規表現を介してこれをしようとしています。
問題は、コントローラのURLのusername:[email protected]
部分を取得する方法が見つからないということです。 Request.RawUrl
を試して、要求オブジェクト内のすべてのデータを検索しましたが、無駄です。
マイコード:
public class ParcelShippingWebHooksController : Controller
{
[ValidateInput(false)]
[HttpPost]
public ActionResult ParcelTrackingWebHookHandler()
{
//Authenticate request
string key = AppSettings.ParcelTrackingWebhookSecretKey;
string url = Request.RawUrl; <---- how get raw url
string strPattern = @"secretkey:(.*)\@";
Match match = Regex.Match(url, strPattern);
if(!match.Success) return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
if(match.Value != key) return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
//Convert request to string
byte[] param = Request.BinaryRead(Request.ContentLength);
string strWebHookDataRequest = Encoding.ASCII.GetString(param);
//perform tracking updates
var webhookAppServ = new ParcelShippingWebHooksAppServ(new InventoryMgmtContext());
var updateTrackingResult = webhookAppServ.UpdateParcelTrackingStatusFromWebHook(strWebHookDataRequest);
if (updateTrackingResult.WasSuccessful)
{
return new HttpStatusCodeResult(HttpStatusCode.OK); // OK = 200
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
}
}
}