Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何在Azure Mobile Services TableController获取模板方法上返回BadRequest?_C#_Linq_Azure_Azure Mobile Services - Fatal编程技术网

C# 我如何在Azure Mobile Services TableController获取模板方法上返回BadRequest?

C# 我如何在Azure Mobile Services TableController获取模板方法上返回BadRequest?,c#,linq,azure,azure-mobile-services,C#,Linq,Azure,Azure Mobile Services,我正在使用Azure移动服务(遵循标准的Azure TodoItems教程),它们提供的最基本的GET方法是: public IQueryable<MyModel> GetAllMyInfo() { return Query(); } 当传递有效的身份验证令牌时,这也起作用但是,如果传递了无效的身份验证头,则currentUser为null,并且查询失败(显然)。因此,我尝试检查null并返回BadRequest或403HTTP代码。然而,一个简单的“return Bad

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

public IQueryable<MyModel> GetAllMyInfo()
{
    return Query(); 
}
当传递有效的身份验证令牌时,这也起作用但是,如果传递了无效的身份验证头,则
currentUser
null
,并且查询失败(显然)。因此,我尝试检查null并返回
BadRequest
403
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); 
}
public IQueryable GetAllMyInfo()
{
//获取当前用户
var currentUser=用户作为服务用户;
if(currentUser==null){
return BadRequest(“数据库已经创建了。”);//这一行给出了一个编译错误,表示我需要强制转换。
}
var ownerId=currentUser.Id;
返回Query(),其中(s=>s.OwnerId==OwnerId);
}

是否有人知道如何检查有效的身份验证令牌并在此方法上返回
403
(它需要
IQueryable
返回类型?
您可以使用
[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); 
}
[授权级别(AuthorizationLevel.User)]
公共IQueryable GetAllMyInfo()
{
//获取当前用户
var currentUser=用户作为服务用户;
var ownerId=currentUser.Id;
返回Query(),其中(s=>s.OwnerId==OwnerId);
}

请注意,对于Azure Mobile Apps SDK(非移动服务),上述属性仅被替换为
[Authorize]

我知道这有点晚了,但将在此处为您和其他可能会发现类似问题的人提供文档。
(虽然同意Matt的观点,403可以/应该通过
[Authorize]
属性实现,但问题在于返回不同的HttpStatusCode或IQueryable)

我有一个类似的场景,我需要验证一些查询参数,然后返回结果或HttpError(在我的例子中,我想要一个包含内容的404)

我找到了两种方法,要么将返回保持为
IQueryable
并抛出
HttpResponseException
,要么将返回更改为
ihttppactionresult
,并使用HttpStatusCode或Ok(数据)返回正常值。
我发现我更喜欢后者,因为抛出异常会破坏调试时的执行,这不是一种非常愉快的开发体验

选项1(首选)
//为API文档生成添加返回注释
[响应类型(类型(IQueryable))]
公共IHttpActionResult GetAllMyInfo()
{
//获取当前用户
var currentUser=用户作为服务用户;
if(currentUser==null){
return BadRequest(“数据库已经创建”);
}
var ownerId=currentUser.Id;
返回Ok(Query(),其中(s=>s.OwnerId==OwnerId));
}
选项2(引发异常)
public IQueryable GetAllMyInfo()
{
//获取当前用户
var currentUser=用户作为服务用户;
if(currentUser==null){
抛出新的HttpResponseException(System.Net.HttpStatusCode.BadRequest)
//或添加内容消息:
抛出新的HttpResponseException(新的System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.BadRequest){
Content=new System.Net.Http.StringContent(“已创建数据库”)
});
}
var ownerId=currentUser.Id;
返回Query(),其中(s=>s.OwnerId==OwnerId);
}
[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); 
}
//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)); 
}
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); 
}