问题标签 [asp.net-web-api2]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
15362 浏览

asp.net - 您如何在 .net WebApi2 应用程序中的 OAuth2 令牌请求中使用额外参数

我在大型 .net MVC 5 Web 解决方案中有一个特定于 api 的项目。我正在使用开箱即用的 WebApi2 模板通过 api 对用户进行身份验证。使用个人帐户进行身份验证,获取访问令牌所需的请求正文是:

这按预期工作。

但是,我需要向脚手架方法“GrantResourceOwnerCredentials”添加第 3 个维度。除了检查用户名/密码之外,我还需要添加一个设备 ID,这旨在限制从用户帐户对特定设备的访问。不清楚的是如何将这些额外的请求参数添加到已经定义的“OAuthGrantResourceOwnerCredentialsContext”中。此上下文当前为用户名和密码腾出了空间,但显然我需要包含更多内容。

我的问题很简单,是否有一种标准方法可以扩展 OWIN OAuth2 令牌请求的登录要求以包含更多数据?而且,您如何在 .NET WebApi2 环境中可靠地做到这一点?

0 投票
1 回答
62 浏览

asp.net - 带有客户端页面的外部身份验证服务

我正在使用 facebook/twitter 登录用户来验证我的 web api。我跟着这个

好吧,这很可能是我的什么。但是我的视图页面将以不同的方式托管。也就是说,用户将使用不同的页面登录,他们将发送一个 ajax 请求,从 api 控制器获取值。

我只会将 Web api 授权给已登录的用户。

0 投票
1 回答
1776 浏览

entity-framework - 在 Web Api 2 OData 端点中使用基于声明的授权保护实体

给定以下控制器

我覆盖ClaimsAuthorizationManager.CheckAccess(...)

Current Principal 这仅对我可以检查in general can是否有用Read Account。但是,如果我想检查某个用户可以阅读哪些帐户,我就迷路了。

假设我有一个经理用户,他应该能够读取他作为经理的所有帐户,而非经理用户应该只能读取他们自己的帐户。

对此是否有最佳实践,或者您以前是否做过类似的事情并给我一些提示?

0 投票
3 回答
1818 浏览

jquery - 在 Web Api 2 中发帖

我正在使用 web api2 在 Asp.Net MVC5 中制作 web api。我发了一个这样的帖子:

模型客户端是这样的:

我在单击按钮时调用了这个 post 方法,只是从客户端的 javascript 中调用,如下所示:

但在响应时,它只会提醒一个空白的警报框。当我通过fire bug检查控制台时出现错误:

为什么会这样??这个Post方法无效吗。请帮我纠正这个...

0 投票
1 回答
685 浏览

c# - 在 Asp.net MVC 中获取帖子数组

我正在编写一个处理 sms-contacts 的 web api2 程序。服务器上有一个 JSON 帖子,如下所示:

在服务器中,我有一个类来处理这个 JSON,如下所示:

控制器是:

现在,当我访问服务器时,它会返回此响应消息:

我无法理解这个问题。我怎样才能将数字存储在不同的表中。 此外,当我使用断点进行调试时, Numbers 和 Msg 值为 null。我如何存储号码。在客户端,帖子很好......

0 投票
1 回答
91 浏览

c# - 在 Post 方法中获取 JSON

我正在使用 c# 在 MVC5 中工作,并且我正在使用 JSON 将数据发布到 Web-api2 服务器,如下所示:

并接受这样的 JSON:

我的 SmsData 模型是这样的:

但我得到了 smsData Null 的 Numbers 属性。为什么会这样...为什么 Numbers 属性会为空。我怎样才能得到数字

0 投票
1 回答
2733 浏览

asp.net-web-api - 扩展 webapi 2.1 的帮助页面以包含其他数据

我在 microsoft高级帮助页面自定义中发现了 Yao 的一篇过时的帖子

我已尝试实施这些步骤,但是我无法在帮助文档中显示我的自定义评论。

在我的

XmlDocumentationProvider.cs

我有修改

我已经更新了

HelpPageModel.cs

包括

我更新的最后一件事是 GenerateApiModel

每次我运行应用程序 responseDocProvider 都是空的。有什么突出的地方看起来我做错了吗?

非常感谢您对问题的帮助

0 投票
1 回答
1420 浏览

c# - Web API 2 Attribute Routing Controller Selection

I use Web API 2 Attribute Routing in my project to provide JSON interface over my data. I am facing weird behaviour of controller selection, not decided yet whether it's a bug or a feature :) Let me describe my approach.

I would like to simulate OData syntax with help of attribute routing (direct OData usage has been refused due to design principles). For example, to get entity with id=5 I use HTTP GET request to URI http://mydomain.com/api/Entity(5) . I expect to use the same URI with HTTP PUT verb to update the entity. This is where the journey begins...

I would like to have separate controller for getting entities (FirstController in the example provided below) and another one for modifying entities (SecondController). Both controllers handles the same URI (e.g. http://mydomain.com/api/Entity(5)) the only difference is HTTP verb used with the URI - GET should be handled by FirstController, PUT should be handled by SecondController. But the URI is handled by none of them; instead HTTP 404 error is returned. When I "merge" GET and PUT actions to only one controller (commented out in FirstController), both verbs are handled correctly. I am using IIS Express and all conventional routes are disabled, only attribute routing is in charge.

It looks like the controller selection process does not work with HTTP verb. In another words, HttpGet and HttpPut attributes just limit action usage but they do not serve as criteria during controller selection. I am not so familiar with MVC / Web API fundamentals, so let me ask you my big question:

Is the behaviour, described herein before, a feature intentionally implemented by MVC / Web API 2 or a bug to be fixed?

If it is considered as a feature, it prevents me to follow design principles. I can live with "merged" controllers but still considering it as a bad practice... Or am I missing something in my train of thought?

My environment setup:

  • Windows 7 (virtual machine using Oracle VirtualBox)
  • Visual Studio 2013
  • .NET 4.5.1
  • Web API 2

The following is implementation of FirstController class:

The following is implementation of SecondController class:

The following is implementation of a console application to test the described behaviour:

For completion, the following are used DTOs:

Thanks in advance for your responses,

Jan Kacina

0 投票
1 回答
1770 浏览

c# - Entity Framework / WebApi 2 权限检查实现

我正在使用 ASP.NET WebApi 2 和 Entity Framework 6.1.0-alpha1 编写一个简单的“Todo App”。我的目标是限制访问,每个用户只能查看/编辑他们自己的待办事项。

例子:

那也行。为删除添加了类似的检查,并在创建时将 CreatorId 设置为当前用户的 ID。但是,我在更新时遇到了问题。

我试过这个:

但是,System.InvalidOperationException它抛出了标记线:

System.InvalidOperationException

附加类型为“ModernWeb.Domain.Models.Todo”的实体失败,因为同一类型的另一个实体已经具有相同的主键值。如果图中的任何实体具有冲突的键值,则在使用“附加”方法或将实体的状态设置为“未更改”或“已修改”时,可能会发生这种情况。这可能是因为某些实体是新实体,尚未收到数据库生成的键值。在这种情况下,使用“添加”方法或“已添加”实体状态来跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改”。

如果我使用 FindByAsync() 删除块,它不会抛出异常。

我也尝试使用_db.Entry(todo).OriginalValue,但找不到有效的语法。

我该如何克服这个问题?对于这种情况有什么最佳实践吗?

0 投票
0 回答
865 浏览

c# - 当我抛出 HttpResponseException 时出错

我正在尝试返回一个状态代码,说明当数据库中不存在用户时未找到该用户

但我收到此错误:

处理 HTTP 请求导致异常。有关详细信息,请参阅此异常的“响应”属性返回的 HTTP 响应。

在此处输入图像描述

这是我发送的回复

有谁知道为什么?

谢谢

编辑

它只发生在我的笔记本电脑上。IIS有问题吗?