Tag: asp.net web api

使用表单发布validationOAuth承载令牌

我使用Web API 2创建了一个基于OData的Web后端。这非常有效,在控制器上使用AuthorizeAttribute。 我希望能够通过标准的html表单和提交上传文件。 我们将OAuth令牌(通常在OData请求的标头中传递)作为隐藏输入字段传递。 问题:如何在控制器中validation?

在Asp.net Web API中处理CORS预检

我的架构中有三个应用程序。 它们位于同一台服务器上,但具有不同的端口号。 A – Token Application (port 4444) – Asp.net WebApi B – API Application (port 3333) – Asp.net WebApi C – UI Application (port 2222) – AngularJS App. 申请流程如下 1- UI项目从令牌应用程序获取令牌(它需要Windows awxrsdsaWeffs12davalidation。)例如: awxrsdsaWeffs12da 2- UI应用程序将此标记放入自定义标头,该标头名为“accessToken” 例如:accessToken: awxrsdsaWeffs12da 3- UI应用程序向API Application Ex发送请求: http:myaddress:3333/api/TheRestServiceHere UI应用程序获得401错误。 哪个发送OPTIONS方法。 (我想预检问题) 在我的web api项目中,我启用了如下所示的Cors。 public static void Register(HttpConfiguration config) { …. […]

如何在使用IdentityServer进行身份validation后获取WebAPI控制器上的用户信息?

在我的客户端应用程序成功validationIdentityServer3后,我无法在WebAPI控制器上获取用户信息。 以下是步骤: 从JavaScript Implicit Client应用程序成功登录“使用配置文件和访问令牌” 我在“ID令牌内容”面板上看到用户的数据 我对我的WebAPI服务进行“呼叫服务”,我在ClaimsPrincipal中看到了许多声明,但无法获取客户端显示的电子邮件,角色等值。 以下是代码和回复。 任何人都可以帮我提供一些如何在WebAPI上获取用户数据的帮助吗?

如何将MultipartMemoryStreamProvider的文件内容作为字节数组获取?

我创建了一个使用FormData将文件上传到Web API控制器的组件。 如何从MultipartMemoryStreamProvider获取文件内容作为字节数组? 这是Web Api方法 public Task<IEnumerable> Post() { if (Request.Content.IsMimeMultipartContent()) { var streamProvider = new MultipartMemoryStreamProvider(); var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<IEnumerable>(t => { if (t.IsFaulted || t.IsCanceled) { throw new HttpResponseException(HttpStatusCode.InternalServerError); } FileDataBO filedata; var fileInfo = streamProvider.Contents.Select(i => { //save to db filedata = new FileDataBO (); filedata.FileName = i.Headers.ContentDisposition.FileName; filedata.FileType = “jpeg”; // […]

ASP.NET Web API中的多个PUT方法

我有一个控制器Groups其中包含以下操作: public GroupModel Get(int ID) public GroupModel Post(CreateGroupModel model) public void Put(PublicUpdateGroupModel model) public void PutAddContacts(UpdateContactsModel model) public void PutRemoveContacts(UpdateContactsModel model) public void Delete(int ID) 而我想要做的是使用标准的REST路由来调用标准的get,post,put,delete mehods。 但是如果动作名称被附加到url,则调用PutAddContacts和PutRemoveContacts ,例如: 获取组/ – 调用Get方法 POST组/ – 调用Post方法 PUT组/ – 调用Put方法 DELETE groups / – 调用Delete方法 PUT groups / addcontacts – 调用PutAddContacts方法 PUT groups / removecontacts – 调用PutRemoveContacts方法 […]

WebAPI帮助页面 – 返回或参数模型/类属性的文档

我正在使用Web API帮助页面与Web API 2(5.0) – 两个最新的Nuget包。 我想帮助文档显示属性的注释,这些类是参数或在HttpResponseMessage的主体中返回。 例如,我有一个像这样的控制器方法: public HttpResponseMessage Post([FromBody] MyClassType1 myClass) { // Business logic removed for clarity return Request.CreateResponse(HttpStatusCode.OK, new MyClassType2()); } 我希望MyClassType1和MyClassType2上的XML注释显示在上述post操作的帮助页面上。 我看过的所有地方,到目前为止看来还没有支持。 但是,我想知道是否有人能通过扩展ApiExplorer,添加到XmlDocumentationProvider等来实现这一点? 我知道注释和属性包含在生成的XML文件中,所以我可以尝试手动解析(所有参数和返回类型都在MyAssemblyName.Models命名空间中,所以我的想法是我可以查找XML具有以该命名空间开头的成员名称的节点。但是,我知道内置的Web API帮助页面具有一些缓存function,所以我更喜欢以某种方式将其与现有function相结合(只需添加它)。 我已经设法通过更新Parameters.cshtml模板来显示参数的类型(仅向下一层): @using System.Reflection @using System.Threading @using System.Web.Http.Description @using Regency.API.Services.Areas.HelpPage @model System.Collections.ObjectModel.Collection NamePropertiesDescriptionAdditional information @foreach (ApiParameterDescription parameter in Model) { string parameterDocumentation = parameter.Documentation ?? “No […]

使用EF和WebApi将父/子对象序列化

我在entity framework中有以下模型: public class Customer { [XmlIgnore] public virtual ICollection Children { get; set; } public string Name { get; set; } } 现在我尝试使用web api序列化它: public class CustomerController:ApiController { public HttpResponseMessage GetAll() { using (var tc = new DataContext()) { List allCustomers = tc.Customers.ToList(); return Request.CreateResponse(HttpStatusCode.OK, allCustomers); } } } 当我这样做并使用POST调用该方法时,我收到以下错误: ‘ObjectContent`1’类型无法序列化内容类型’application / json的响应主体; 字符集= […]

c#和Asp.net web api中的URL编码

我有一个ASP.NET web api ,它接收Web请求并返回Json数据。 浏览到此url: http://1.2.3.4/api1/api/values/mypartname将返回以下json字符串: { \”PartName\”: \”mypartname\”, \”PartDes\”: \”53.6X53.6APA/ALIM1NOTPAK\”, \”PartLocation\”: \”A36\” } 但是当发送包含这样的空格或引号的部件名称时: http://1.2.3.4/api1/api/values/my part na”me : http://1.2.3.4/api1/api/values/my part na”me得到404 – File or directory not found.错误。 我正在使用.NET 4 Console application使用json : static void Main(string[] args) { try { string partName = “TAPE 56 3M 3/4\””; WebRequest wr = WebRequest.Create(“http://1.2.3.4/api1/api/values/” + HttpUtility.UrlEncode(partName)); wr.Credentials = […]

HttpResponseMessage无法在Web Api(.NET 4.5)中工作

我阅读http://www.asp.net/web-api/overview/security/working-with-ssl-in-web-api并尝试使用该页面中的代码: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.Http; using System.Web.Http.Filters; using System.Web.Http.Controllers; public class RequireHttpsAttribute : AuthorizationFilterAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps) { actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden) { ReasonPhrase = “HTTPS Required” }; } else { base.OnAuthorization(actionContext); } } } 当我构建时,我没有收到错误,但运行时给出了这个错误: CS0012:类型’System.Net.Http.HttpRequestMessage’在未引用的程序集中定义。 […]

我可以从运行在其上的Web API应用程序将文件写入服务器计算机上的文件夹吗?

我在我的Web API应用程序中使用此代码来写入CSV文件: private void SaveToCSV(InventoryItem invItem, string dbContext) { string csvHeader = “id,pack_size,description,vendor_id,department,subdepartment,unit_cost,unit_list,open_qty,UPC_code,UPC_pack_size,vendor_item,crv_id”; int dbContextAsInt = 0; int.TryParse(dbContext, out dbContextAsInt); string csvFilename = string.Format(“Platypus{0}.csv”, dbContextAsInt); string csv = string.Format(“{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}”, invItem.ID, invItem.pksize, invItem.Description, invItem.vendor_id, invItem.dept, invItem.subdept, invItem.UnitCost, invItem.UnitList, invItem.OpenQty, invItem.UPC, invItem.upc_pack_size, invItem.vendor_item, invItem.crv_id); string existingContents; using (StreamReader sr = new StreamReader(csvFilename)) { existingContents = sr.ReadToEnd(); } […]