簡體   English   中英

在Web API中,驗證API調用,在請求有效或用戶繞過某些調用的最佳方法是什么?

[英]What is best way to validate API calls, weather the request is valid or the user bypassing some calls, in web api?

謝謝,我正在Dot.Net內核中開發一種Rest API。 訪問時,我需要確保API調用的安全。

我有10個其余的API調用,這里很少幾個,

  1. ValidateUser,
  2. UploadDocument
  3. 驗證文件
  4. 批准文件
  5. 符合條件的個人貸款。

這些調用的順序應為順序順序,例如上面的項目符號:1-> 2-> 3-> 4-> 5-> 6。

如果繞過者在“ 5. EligibleForPersonalLoan”呼叫之后發出呼叫請求“ 2.UploadDocument”,並且此請求是錯誤的,並且在這種情況下,用戶已經繞過了兩個呼叫(3和4),因此在這里我想返回“無效的請求” ' 錯誤信息。 那么如何處理這種情況。

您可以通過引入一個名為“ LoanStatus”的新枚舉輕松地對其進行管理。

public class Loan 
{

    public long Id { get; set; }

    public virtual User User{ get; set; }

    public virtual List<Document> Documents{ get; set; }

    public LoanStatus LoanStatus{ get; set; }

}

public enum LoanStatus
{       
   UserValidated,
   DocumentUploaded,
   DocumentVerified,
   DocumentApproved,
   LoanEligibility...
}

每次調用WebApi時,您都要檢查LoanStatus屬性,並查看其是否處於預期狀態,否則將拋出禁止的請求。 如果狀態是預期的狀態,則應執行所有邏輯,然后更改實體的狀態。

[HttpGet]
[Route("verifydocument/{loadId:long}")]
public IHttpActionResult VerifyDocument(long loadId)
    {
        try
        {
            var loan= _loanService.FindLoanById(loadId);
            if (loan.LoanStatus!=null && loan.LoanStatus.Equals(LoanStatus.DocumentUploaded)
            //Do logic for the verifyDocument and update the LoanStatus to DocumentVerified
            {  
               return Ok(loanUpdated);
            }
            return Forbid();
        }
        catch (Exception exception)
        {
            return InternalServerError(exception);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM