asp.net-web-api2

Enable CORS for Web Api 2 and OWIN token authentication

谁说胖子不能爱 提交于 2019-11-29 20:41:38
I have an ASP.NET MVC 5 webproject (localhost:81) that calls functions from my WebApi 2 project (localhost:82) using Knockoutjs, to make the communication between the two projects I enable CORS. Everything works so far until I tried to implement OWIN token authentication to the WebApi. To use the /token endpoint on the WebApi, I also need to enable CORS on the endpoint but after hours of trying and searching for solutions it is still now working and the api/token still results in: XMLHttpRequest cannot load http://localhost:82/token. No 'Access-Control-Allow-Origin' header is present on the

Multiple controllers with same URL routes but different HTTP methods

房东的猫 提交于 2019-11-29 19:30:30
问题 I've got a following two controllers: [RoutePrefix("/some-resources") class CreationController : ApiController { [HttpPost, Route] public ... CreateResource(CreateData input) { // ... } } [RoutePrefix("/some-resources") class DisplayController : ApiController { [HttpGet, Route] public ... ListAllResources() { // ... } [HttpGet, Route("{publicKey:guid}"] public ... ShowSingleResource(Guid publicKey) { // ... } } All three actions got in fact three different routes: GET /some-resources POST

Post JSON string to WEB API

我们两清 提交于 2019-11-29 19:13:08
问题 I have an ASP.NET WEB-API 2 app witch needs to have a POST method that accepts a JOSN string with unknown structure from javascript . I enabled cors and GET methods works fine, however when sending JSON from the client the api's method parameter is always null . This is my api method: //parameters i tried: //[FromBody]string model //[FromBody]dynamic model //dynamic model public HttpResponseMessage Post(string model) { return new HttpResponseMessage() { Content = new StringContent("POST: Test

MVC5, WebAPI2 and Ninject Parameterless constructor error

岁酱吖の 提交于 2019-11-29 14:50:59
So I have the exact opposite problem as MVC5, Web API 2 and Ninject I have a new MVC5/WebAPI2 project, that has both "Controller"s and "ApiControllers". I'm using the latest unstable version of Ninject.Web.WebAPI with no code changes to NinjectDependencyResolve.cs and Ninject.WebCommom.cs (besides binding my dependency) the ApiController's constructor injection works. However, when I call a MVC Controller I get: No parameterless constructor defined for this object. Tim The issue is you need a Dependency Resolver for both MVC and WebAPI. Depending on which set of Ninject libraries you use, you

Upload file using PUT verb in ASP.Net Web Api 2

怎甘沉沦 提交于 2019-11-29 14:37:11
问题 I would like to expose an ASP.Net Web Api 2 action using the HTTP PUT verb to upload files. This is coherent with our REST model as the API represents a remote file system (similar to WebDAV, but really simplified), so the client chooses the resource names (thus PUT is ideal and POST is not a logical choice). The Web Api documentation describes how to upload files using multipart/form-data forms, but does not describe how to do it using PUT methods. What would you use to test such an API

How to validate JWT Token in aspnet.core web api?

浪尽此生 提交于 2019-11-29 14:34:54
I have created custom middleware class which validates the JWT token. I am calling this method before app.AddMvc() in configure method. *** I would like to know what are the things that I should add to Configuration services to authenticate my web API using JWT? I have added [Authorize] in my Controller class Do I need to call my middleware class which validates the JWT token first in Configure method? or I should call App.UseAuthentication() I am using the following order : app.UseAuthentication(); app.MessageHandlerMiddleware(); app.UseMvc(); I am new to .net web API implementation. Could

Returning generated CSS from an MVC5 or Web API 2 controller action

岁酱吖の 提交于 2019-11-29 12:45:03
In our multi-tenant application we have a need to customize the styles used per-tenant. We currently plan to do so using LESS and variables in the following way on the client : Download dependent LESS files from server Call web service to get configuration object Form string of valid LESS with variables defined Use less.js compiler to compile LESS based on these variables and the fixed LESS files from step 1 This approach has a number of downsides: Clients can behave badly Some browsers have problems with less.js Compilation takes time We would instead like to take care of this work on the

Web API 2 - Implementing a PATCH

馋奶兔 提交于 2019-11-29 11:56:56
问题 I currently have a Web API that implements a RESTFul API. The model for my API looks like this: public class Member { public string FirstName { get; set; } public string LastName { get; set; } public DateTime Created { get; set; } public DateTime BirthDate { get; set; } public bool IsDeleted { get; set; } } I've implemented a PUT method for updating a row similar to this (for brevity, I've omitted some non-relevant stuff): [Route("{id}")] [HttpPut] public async System.Threading.Tasks.Task

Issue with sending XML to Web Api via http Post request

帅比萌擦擦* 提交于 2019-11-29 11:47:38
I would like to send the following XML document to my Web Api 2 controller; however, the [FromBody] parameter is always null. Here is the request XML body: <razorInbound server="PortfolioExposureServer" request="lookupRiskPointRecord"> <caller>RazorClient</caller> <responseMode>xml</responseMode> <body> <conditions> <fromOffset>0</fromOffset> <top>100</top> <condition> <keyPath> <keyElement nodeOffset='1'>Currency</keyElement> <keyElement nodeOffset='2'>ID</keyElement> </keyPath> <lookupValue>USD</lookupValue> </condition> </conditions> </body> </razorInbound> Using Postman to send the request

Override AccessTokenExpireTimeSpan

╄→尐↘猪︶ㄣ 提交于 2019-11-29 09:51:01
Is possible to override the default AccessTokenExpireTimeSpan for a specific ticket on a custom OAuthAuthorizationServerProvider? The default expiration time for all other tickets is 15 minutes. public public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { ... var ticket = new AuthenticationTicket(identity, properties); if (condition) { ticket.Properties.IssuedUtc = DateTime.UtcNow; ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddDays(14); } context.Validated(ticket); } The generated token with condition == true has the default expiration time