首先,我是 MVC 的新手,所以請耐心等待。我正在關注位于此處的 Twilio 示例,以驗證來自 Twilio 的狀態回呼是否真實:https ://www.twilio.com/docs/usage/tutorials/how-to-secure-your-csharp-aspnet-app-by -validating-incoming-twilio-requests#create-a-custom-filter-attribute
按原樣提供的代碼應該由 Twilio 審查,因為建構式名稱中有一個拼寫錯誤,因為它們有兩次“RequestAttribute”。
我的問題是,當我把它放在我的控制器上時,我一生都無法解決這個屬性。我注意到示例操作過濾器有一個以“.Filters”結尾的命名空間,我注意到我的 MVC 應用程式沒有“過濾器”目錄。一些谷歌搜索表明這可能是 MVC4 和 5 之間的差異——據我所知,我正在使用 MVC5,因為這是 System.Web.Mvc 參考中列出的版本號(特別是 5.2.2.0 版,如果這很重要)。
無論如何,Twilio 在此處鏈接到有關創建自定義操作過濾器的 Microsoft 檔案:https : //docs.microsoft.com/en-us/previous-versions/aspnet/dd410056(v= vs.98)?redirectedfrom=MSDN
Microsoft 建議直接在“Controllers”目錄中創建 Action Filter 類 - 這就是我放置我的位置。以下是該類當前的讀取方式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Twilio.Security;
using System.Net;
using System.Web.Mvc;
using System.Web.Mvc.Filters;
namespace SMSStatusWS.Controllers
{
public class ValidateTwilioRequestAttribute : System.Web.Mvc.ActionFilterAttribute
{
private readonly RequestValidator _requestValidator;
public ValidateTwilioRequestAttribute()
{
var authToken = "<--------REMOVED------->";
_requestValidator = new Twilio.Security.RequestValidator(authToken);
}
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
var context = actionContext.HttpContext;
if (!IsValidRequest(context.Request))
{
actionContext.Result = new HttpStatusCodeResult(HttpStatusCode.Forbidden);
}
base.OnActionExecuting(actionContext);
}
private bool IsValidRequest(HttpRequestBase request)
{
var signature = request.Headers["X-Twilio-Signature"];
var requestUrl = request.Url.AbsoluteUri;
return _requestValidator.Validate(requestUrl, request.Form, signature);
}
}
}
然后我嘗試使用它......
[HttpPost]
[ValidateTwilioRequest ]
public ActionResult UpdateSmsStatus(string dbContext, string smsQueueId)
{
// Controller code here...
}
該屬性[ValidateTwilioRequest ]在 Visual Studio 中以紅色突出顯示......意味著它無法決議它(我猜)但我不確定為什么我的控制器類和操作過濾器類中的命名空間相同......
namespace SMSStatusWS.Controllers
所以在這一點上,我不確定這里出了什么問題。據我所知,自定義操作過濾器已正確創建,我正在使用它作為 Twilio 顯示,但它不起作用。即使過濾器似乎沒有解決,專案構建并運行......但當然它永遠不會命中操作過濾器中的代碼。
Is there some other way I need to reference this Action Filter that the docs are not telling me? Thoughts on what to try or look for that I may be overlooking?
UPDATE 10/13/21: I have made some progress... turns out the attribute not resolving was related to Visual Studio. Not able to figure out why it wasn't resolving, I closed the solution and restarted Visual Studio and when I reopened it, it started resolving the attribute immediately. However, I'm not positive it's working as I can't hit a breakpoint in the OnActionExecuting override and Visual Studio says no symbols have loaded.
So new question now, how do you debug custom Action Filters? Shouldn't I be able to step into the code with the debugger?
uj5u.com熱心網友回復:
您可以嘗試在 OnActionExecuting 函式中使用 System.Diagnostics.Debug.WriteLine 并使用它而不是斷點。這應該寫到輸出視窗,并讓您知道該代碼是否正在運行。添加該代碼后,您的 OnActionExecuting 函式將如下所示。
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
string controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string action = actionContext.ActionDescriptor.ActionName;
System.Diagnostics.Debug.WriteLine("Controller-" controller ", Action-" action);
var context = actionContext.HttpContext;
if (!IsValidRequest(context.Request))
{
actionContext.Result = new HttpStatusCodeResult(HttpStatusCode.Forbidden);
}
base.OnActionExecuting(actionContext);
}
uj5u.com熱心網友回復:
哇...今天學到了一些關于 Visual Studio 和除錯的知識。我為所有這些創建的 MVC 專案將專案輸出設定為比我的專案高 2 級的 bin 目錄。我將其稱為“全域 bin”——我們使用我們的全域 bin 作為許多其他專案將其輸出寫入的地方,并且在我們的夜間構建中參考了一個位置。
我注意到我的本地專案 bin 目錄中有一個舊的 DLL 和 PDB 檔案,大概來自將輸出目錄更改為我們的全域 bin 之前的構建(全域 bin 具有當前的 dll 和 pdb)。除錯時,我會注意到除錯器會從“臨時 Asp.Net 檔案”路徑加載 dll 和 pdb(在 Visual Studio 中除錯時,您可以在“模塊”視窗中找到此資訊) - 這個臨時目錄具有相同的舊版本的 pdb 在其中,這就是我最終無法除錯我的操作過濾器屬性的原因——它有一個過時的 pdb,其中沒有我最新代碼更改的符號。
我現在的創可貼是將我的專案的輸出目錄設定回它自己的本地 bin 目錄,并在 web.config 中禁用 shadowCopyBinAssemblies - 這可以防止除錯器使用臨時 Asp.Net 檔案目錄中的過時 pdb 和而是使用本地 bin 中的 pdb。
在 web.config 中看起來像這樣...
<system.web>
<hostingEnvironment shadowCopyBinAssemblies="false"/>
<system.web
我很困惑為什么 Visual Studio 不將 dll 和 pdb 從我們定義的全域 bin(當它定義為專案的輸出目錄時)復制到臨時 Asp.Net 檔案目錄。這將解決所有這些問題,但我不知道是否可以在任何地方進行配置。
一旦我將其更改為輸出到專案的 bin 目錄并禁用 web.config 中的 shadowCopy,除錯器就會從正確的 dll 和 pdb 加載符號,我的斷點立即命中。
我想我會添加一個構建后事件或其他東西來將 dll 和 pdb 檔案復制到我們的全域 bin 中,這樣就不會破壞我們的夜間構建,但這感覺就像一個黑客。如果無法更改臨時 Asp.Net 檔案目錄的默認 shadowCopy 設定,我不知道還能做些什么。這可能是一個 Visual Studio 錯誤...總是從專案箱中進行 shadowCopying,即使輸出配置為在其他地方寫入?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/321744.html
下一篇:縮放時更改地標名稱的字體大小
