【问题标题】:How can I return a BadRequest on Azure Mobile Services TableController GET template method?如何在 Azure 移动服务 TableController GET 模板方法上返回 BadRequest?
【发布时间】:2015-11-20 05:43:05
【问题描述】:

我正在使用 Azure 移动服务(遵循标准的 Azure TodoItems 教程),他们提供的最基本的 GET 方法是:

public IQueryable<MyModel> GetAllMyInfo()
{
    return Query(); 
}

这可行,但我正在尝试对其进行扩展,以便该方法仅返回经过身份验证的用户的 MyModel 数据(由移动服务 API 调用的 X-ZUMO-AUTH 身份验证标头标准标识)。所以我修改了代码:

public IQueryable<MyModel> GetAllMyInfo()
{
    // Get the current user
    var currentUser = User as ServiceUser;
    var ownerId = currentUser.Id;
    return Query().Where(s => s.OwnerId == ownerId); 
}

这在传递有效的身份验证令牌时也有效。 但是,如果传递了无效的 auth 标头,则 currentUsernull,并且查询失败(显然)。所以我试图检查 null 并返回 BadRequest403 HTTP 代码。然而,一个简单的 `return BadRequest("Invalid authentication") 会产生编译错误:

public IQueryable<MyModel> GetAllMyInfo()
{
    // Get the current user
    var currentUser = User as ServiceUser;
    if(currentUser == null) {
        return BadRequest("Database has already been created."); // This line gives a compilation error saying I need a cast.
    }
    var ownerId = currentUser.Id;

    return Query().Where(s => s.OwnerId == ownerId); 
}

有谁知道如何检查有效的身份验证令牌并在此方法上返回 403(需要 IQueryable 返回类型?

【问题讨论】:

    标签: c# linq azure azure-mobile-services


    【解决方案1】:

    您可以在此方法上使用[AuthorizeLevel] 属性来指示必须存在有效​​令牌才能调用该方法。如果没有,它将返回 401。

    所以你的完整方法是:

    [AuthorizeLevel(AuthorizationLevel.User)]
    public IQueryable<MyModel> GetAllMyInfo()
    {
        // Get the current user
        var currentUser = User as ServiceUser;
        var ownerId = currentUser.Id;
        return Query().Where(s => s.OwnerId == ownerId); 
    }
    

    请注意,对于 Azure 移动应用 SDK(不是移动服务),上述属性仅替换为 [Authorize]

    【讨论】:

      【解决方案2】:

      我知道这有点晚了,但会在这里为您和其他可能会寻找类似问题的人记录。
      (虽然同意 Matt 可以/应该使用 [Authorize] 属性实现 403,但问题是关于返回不同的 HttpStatusCode 或 IQueryable)

      我有一个类似的场景,我需要验证一些查询参数并返回我的结果或 HttpError(在我的情况下,我想要一个带有内容的 404)。

      我找到了 2 种方法,将返回值保持为 IQueryable&lt;T&gt; 并抛出 HttpResponseException 或将返回值更改为 IHttpActionResult 并使用 HttpStatusCode 或 Ok(Data) 返回正常。
      我发现更喜欢后者,因为在调试时抛出异常会破坏执行,而不是非常愉快的开发体验。

      选项 1(首选)

      //Adding Return annotation for API Documentation generation
      [ResponseType(typeof(IQueryable<MyModel>))]
      public IHttpActionResult GetAllMyInfo()
      {
          // Get the current user
          var currentUser = User as ServiceUser;
          if(currentUser == null) {
              return BadRequest("Database has already been created.");
          }
          var ownerId = currentUser.Id;
      
          return Ok(Query().Where(s => s.OwnerId == ownerId)); 
      }
      

      选项 2(抛出异常)

      public IQueryable<MyModel> GetAllMyInfo()
      {
          // Get the current user
          var currentUser = User as ServiceUser;
          if(currentUser == null) {
              throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest)
              // Or to add a content message:
              throw new HttpResponseException(new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) {
                  Content = new System.Net.Http.StringContent("Database has already been created.")
              });
          }
          var ownerId = currentUser.Id;
      
          return Query().Where(s => s.OwnerId == ownerId); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多