我已经为这个问题搜索了几个小时,但似乎在任何地方都找不到任何答案
我已经构建了一个 ASP.NET WebAPI,它接受 JSON GET/POST 请求,当我使用 fiddler 或 google chrome 的高级休息客户端扩展时,它可以工作 A1。
现在我必须采用现有的 android 应用程序并修改它以与我的 WebAPI 一起使用。我已经清除了所有不需要的垃圾,但我仍然无法发布。GET 工作正常,我收到我的响应字符串,一切正常,但 POST 返回 400 Bad Request。
我的 webApi 控制器需要一个 ProblemReports 对象。问题报告是由我制作的,它是这样的:
public class ProblemReports
{
[Key]
public int problem_id { get; set; }
[Required]
public Nullable<int> request_type { get; set; }
[Required]
public Nullable<int> problem_type_id { get; set; }
public String picture_url { get; set; }
public contact_information contact_information { get; set; }
public problem_location problem_location { get; set; }
public bool …Run Code Online (Sandbox Code Playgroud) 我已经构建了一个用于用户登录的WebAPI,如果用户提供了正确的用户名和密码,WebAPI 可以生成访问令牌。我的问题是如何将用户角色信息也传递给 MVC 应用程序。
例如,
我在下面有一个 MVC 应用程序控制器,如何从 Web API 传递角色“管理员、用户编辑器”?我知道我可以使用另一个 WebAPI 调用来检查用户角色,但这不是一个好主意。
[Authorized("Admin,UserEditor")]
ActionResult EditUser(int? Id)
{
........
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在标准 Web API 2.2 中使用 OData 过滤和分页功能ApiController。为此,我必须重写请求 URL 以符合 OData v4 标准。我的控制器看起来像这样:
public GridPage Get([FromUri] GridSearchCriteria criteria)
{
Request.RequestUri = ... // convert querystring to OData v4
var context = new ODataQueryContext(MyEdmModel.Instance, typeof(Delivery), null);
ODataQueryOptions<Delivery> options = new ODataQueryOptions<Delivery>(context, Request);
IQueryable<Delivery> deliveries = ... // use EF to load deliveries from DB
var result = (IQueryable<Delivery>)options.ApplyTo(deliveries); // BTW, I wonder why there is no generic overload of ApplyTo?
// fill and return a GridPage
...
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都按预期运行良好。
现在,我对过滤项目的总数感兴趣,因此我已将其添加 …
我有一个网络应用程序。在主页中,用户将输入凭据,系统应根据 Azure AD 进行验证并继续进行。
当我使用本机应用程序并使用 时UserCredentials,它会验证用户,但如果我对 WebAPI 使用相同的方法,则会引发异常
请求正文必须包含以下参数:“client_secret 或 client_assertion”
当我使用 WebAPI using 时clientCredentials,它会生成 accessToken,它不会验证用户凭据。我还尝试在随后的调用中将凭据作为 httpclient 标头的一部分传递,尽管凭据错误,但它仍然有效。
string AzureADSTSURL = "https://login.windows.net/{0}/oauth2/token?api-version=1.0";
string GraphPrincipalId = "https://graph.windows.net";
string userid = "userid";
string password = "pass";
string tenantId = "axxx"; // webapi
string clientId = "bxxx";
string clientSecret = "cxxx";
string authString = String.Format(AzureADSTSURL, tenantId);
var context = new AuthenticationContext(authString);
UserCredential userCredentials = new UserCredential(userid, password);
AuthenticationResult authenticationResult = context.AcquireToken(GraphPrincipalId.ToString(), clientId, userCredentials); // this works only if the …Run Code Online (Sandbox Code Playgroud) OData 端点是使用 ASP.NET Web API 2.0 创建的
尝试在 ODataController 中创建一个查询拦截器,如下面的代码所示:
public class AVMItemController : ODataController
{
ADWAppContext sampleADW = new ADWAppContext("Server=XXX;Database=XXX;User ID=XXX;password=xxx;Trusted_Connection=false;Encrypt=true");
// GET: odata/AVM
[EnableQuery(PageSize=25)]
public IQueryable<ADWAppContext.AVMItem> GetAVMItems()
{
return sampleADW.AVMItems.AsQueryable<ADWAppContext.AVMItem>();
}
[QueryInterceptor("AVMItems")]
public Expression<Func<ADWAppContext.AVMItem, bool>> FilterAVMItems()
{
return avm => avm.avmId > 1000;
}
}
Run Code Online (Sandbox Code Playgroud)
所有 OData 查询的处理都没有考虑 Query Interceptor 中提到的谓词。我在查询拦截器方法中有一个从未命中的断点。
asp.net odata asp.net-web-api asp.net-web-api2 queryinterceptor
正如我的问题中所述,我想知道c# 异常中Argument和ArgumentNull异常之间的逻辑区别是什么。
谢谢
我在 Mvc 中使用 webapi 服务我已经创建了 Api 到 mvc 的引用,但是在编写代码后我收到了这个错误
类型“System.Web.Http.ApiController”是在未引用的程序集中定义的。您必须添加对程序集“System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”的引用
我也安装了这个包
Install-Package Microsoft.AspNet.WebApi.Core -version 5.2.3
Run Code Online (Sandbox Code Playgroud) 我在 web api 中使用了一个静态并发字典,它可以被用户访问。
我正在使用以下方法:这些方法都是线程安全的吗?或者我必须添加lock()即使它是ConccurentDictionary?
基本上,它是由整个用户访问的,然后它应该能够相应地工作,因为该字典包含每个用户的集合并且它依赖于此。
static ConcurrentDictionary<string, SApp> SAppFarm= new ConcurrentDictionary<string, SApp>();
.TryRemove(_sessionUser,out s);
.TryAdd(_sessionUser, r);
.GetOrAdd(sessionUser, application);
Run Code Online (Sandbox Code Playgroud) c# asp.net multithreading concurrentdictionary asp.net-web-api
我的 ASP.NET Web API 应用程序中有一个自定义验证属性,用于检查日期是否在未来(不允许在未来的日期)。这是代码:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
var date = (DateTime)value;
if (date != null)
{
if(date.Date > DateTime.UtcNow.Date)
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName });
}
}
}
return ValidationResult.Success;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何进行比较,因此它适用于所有时区。使用DateTime.UtcNow不是解决方案,因为如果客户端和服务器在同一个时区,在接近午夜的时间里,日期将是后一天。而且,当然DateTime.Now不适用于其他时区。那么,有什么解决办法呢?
更新:
在我的WebApiConfig.cs文件中,我有这个代码来设置DateTimeZoneHandling为Utc:
jsonFormatter.SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
Run Code Online (Sandbox Code Playgroud)
这将格式化 JSON 响应中的日期,如下所示:“2018-03-02T00:00:00Z”。
而且,来自客户端的所有 DateTime 值都将其Kind …
我正在尝试使用 spa 和 webapi 进行 azure 活动目录登录
按照链接中的源代码
https://github.com/Azure-Samples/active-directory-angularjs-singlepageapp-dotnet-webapi/issues/30
但我收到以下消息作为回应
请求 ID:f106b5ad-6392-4c41-8fa2-473629870700 相关性 ID:6abe676b-e3d0-41e2-8255-829dff79b6b0 时间戳:2018-04-120550DS6 消息:有效资源 客户端已请求访问未在客户端应用程序注册中请求的权限中列出的资源。客户端应用程序 ID:8f4b3482-68c1-42b1-b4dd-ea1e0ad9a65e。来自请求的资源值: 。资源应用 ID:00000002-0000-0000-c000-000000000000。来自应用注册的有效资源列表:c7aee132-6d62-4d80-9511-057c9420fd2b。
asp.net-web-api ×10
c# ×6
asp.net ×4
odata ×2
android ×1
asp.net-mvc ×1
azure ×1
credentials ×1
datetime ×1
json ×1
timezone ×1
utc ×1