【问题标题】:Pass and verify data between different razor pages在不同剃须刀页面之间传递和验证数据
【发布时间】:2019-05-17 23:08:45
【问题描述】:

我正在使用 Asp.net Core2.1 Razor 页面技术。

我想询问有关在剃须刀页面之间传递和验证参数的问题。 请不要说我问的是概念而不是编码:

现在假设我有博客,并且每个博客都归一个可以管理它的用户所有。

用户使用此 url 进入管理页面:

https://localhost:44368/blogs/1/settings

如你所见,博客的 id 在 url 中:

public async Task<IActionResult> OnGetAsync(int? id)
{
        // here i check that the blog is exist by the id
        // and i check if the current user own the blog            
}

然后在设置页面我有几个页面的链接,例如(文章) 并且用户可以管理这些文章。

https://localhost:44368/blogs/1/settings/articles

如你所见,我仍然在 url 中有博客 ID:

public async Task<IActionResult> OnGetAsync(int? id)
{
    // now this function in the articles page
    // again i check if the blog is exist
    // and again i check if the current user can manage the blog or not                             
}

这是正确和好的做法吗?在每个页面中检查和验证

或者我应该只在进入设置页面时检查?

或者我应该考虑一种方法,当用户进入设置页面时只检查一次,然后用户不能根据第一次检查进入其他页面!

【问题讨论】:

    标签: c# asp.net asp.net-core-2.1 razor-pages


    【解决方案1】:

    保持 Web 端点stateless 是一种很好的做法。

    因此,您将 Id 传递给每个子操作并验证此输入的方法是正确的。

    要实现只检查一次 Id 的另一种方法,您需要在操作之间传递状态,例如作为会话状态。这种方法不太灵活。也许您希望将来有一天能够从博客详细信息以外的其他页面打开设置?

    还要记住,仅仅因为用户没有看到页面上的某个链接,没有什么可以阻止她进入,例如https://localhost:44368/blogs/1/settings/articles直接进入浏览器地址栏。因此,在任何情况下,您都需要对每个操作进行一些验证。

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 2020-04-30
      • 2018-05-30
      • 2020-01-30
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多