简体   繁体   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?

Thanks, I am developing one rest API in Dot.Net core. 谢谢,我正在Dot.Net内核中开发一种Rest API。 I need to secure my API calls while accessing. 访问时,我需要确保API调用的安全。

I have 10 rest API calls, here few of them, 我有10个其余的API调用,这里很少几个,

  1. ValidateUser, ValidateUser,
  2. UploadDocument UploadDocument
  3. VerifyDocument 验证文件
  4. ApproveDocument 批准文件
  5. EligibleForPersonalLoan. 符合条件的个人贷款。

The order of these calls should be sequential order, what bulleted above Example: 1 -> 2 -> 3 -> 4 -> 5 -> 6. 这些调用的顺序应为顺序顺序,例如上面的项目符号:1-> 2-> 3-> 4-> 5-> 6。

if byepasser make a call request "2.UploadDocument" after the "5. EligibleForPersonalLoan" call, and this request is wrong and in this scenario the user has byepassed two calls(3 and 4), so here i want to return 'invalid request' error message. 如果绕过者在“ 5. EligibleForPersonalLoan”呼叫之后发出呼叫请求“ 2.UploadDocument”,并且此请求是错误的,并且在这种情况下,用户已经绕过了两个呼叫(3和4),因此在这里我想返回“无效的请求” ' 错误信息。 So how to handle this scenario. 那么如何处理这种情况。

You could easily managed it by introducing a new enum called "LoanStatus" 您可以通过引入一个名为“ 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...
}

Each time a WebApi is called you check the LoanStatus property and see if it's in the expected status otherwise you throw a forbidden request. 每次调用WebApi时,您都要检查LoanStatus属性,并查看其是否处于预期状态,否则将抛出禁止的请求。 If the status is the one expected you do all your logic and then you change the status of the entity. 如果状态是预期的状态,则应执行所有逻辑,然后更改实体的状态。

[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