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