asp.net-web-api2

Calling service/repository methods in ASP.Net Core middleware

ぐ巨炮叔叔 提交于 2019-12-05 16:30:53
ASP.Net Core noob here...I am using an ASP.Net Core WebAPI core project using DNX451 with EF 6. I have a requirement to implement API Key auth in our service. To do this I have created middleware that gets information from the request and proceeds with authentication. It is SUPPOSED to go to the database, get the key to match, and then return and do the validation. Here is the middleware implemented to look at the context and get the APIKey AuthenticationHandler public class AuthorizationHandler { private readonly RequestDelegate _next; private IAuthenticationService _authenticationService;

“Cannot be determined because there is no implicit conversion” with ternery if return

三世轮回 提交于 2019-12-05 14:58:51
问题 I have the following ASP.NET Web Api 2 action with a ternary if return: [HttpDelete] public IHttpActionResult Delete() { bool deleted; // ... return deleted ? this.Ok() : this.NotFound(); } I receive a Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Web.Http.Results.OkResult' and 'System.Web.Http.Results.NotFoundResult' when they both implement IHttpActionResult . However if I remove the ternary if, the compiler is happy: if (deleted

Does IExceptionLogger deprecate the need for ExceptionFilterAttribute in Web API 2?

核能气质少年 提交于 2019-12-05 14:51:16
问题 Following the discussion from the official documentation on implementing an IExceptionLogger (http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling) which links to the (now dated?) article on implementing an ExceptionFilterAttribute (http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling), is there any reason to register a global ExceptionFilterAttribute if you register a service for IExceptionLogger ? I did and when debugging an

Web Api Controller in other project, route attribute not working

风流意气都作罢 提交于 2019-12-05 13:51:46
问题 I have a solution with two projects. One Web Api bootstap project and the other is a class library. The class library contains a ApiController with attribute routing. I add a reference from web api project to the class library and expect this to just work. The routing in the web api is configured: config.MapHttpAttributeRoutes(); The controller is simple and looks like: public class AlertApiController:ApiController { [Route("alert")] [HttpGet] public HttpResponseMessage GetAlert() { return

how to hide metadata in web api 2, odata

*爱你&永不变心* 提交于 2019-12-05 13:03:39
I have defined odata route using MapODataServiceRoute in my WebApiConfig . config.Routes.MapODataServiceRoute("CompanyoOdata", "odata", GetImplicitEdm(config)); private static IEdmModel GetImplicitEdm(HttpConfiguration config) { ODataModelBuilder builder = new ODataConventionModelBuilder(config, true); builder.EntitySet<Company>("Company"); builder.EntitySet<Photo>("Photos"); builder.EntitySet<Country>("Country"); return builder.GetEdmModel(); } The data service works just fine. But I want to achieve few things. I don't want to expose my metadata or associations because i'm using it internally

Web API 2 AccessFailedCount not Incrementing When using Token Based Authentication

泪湿孤枕 提交于 2019-12-05 12:02:26
I am using Webapi with Identity2.0 AccessFailedCount, LockoutEndDateUtc is not incermenting on Invalid UserName and Password. I have implement Token Based Authentication provided by WebAPI. Please help . here is code Snippet using (UserManager<ApplicationUser> userManager = userManagerFactory) { ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } if (await userManager.IsLockedOutAsync(user.Id)) { context.SetError("lock_out", "The account is locked.");

OWIN Authentication and Custom Response

风格不统一 提交于 2019-12-05 11:18:01
I create a custom BasicAuthenticationMiddleware that use a BasicAuthenticationHandler to Authenticate requests from client to WebAPI. The BasicAuthenticationHandler derives from the AuthenticationHandler< TOptions > base class. Everything works fine and I implemented the AuthenticateCoreAsync where the logic to authenticate happens ApplyChallengeResponseAsync where the logic, in case of not authenticated requests, sends the WWW-Authenticate header to the client. What I would like to achieve now is to set a Custom Body in the Response (IOwinResponse, inside the ApplyChallengeResponseAsync, with

Web API 2 project and MVC 5 Website project in same domain

非 Y 不嫁゛ 提交于 2019-12-05 10:42:21
Technologies used: BreezeJS OData Web API 2 MVC 5 IDE: Visual Studio 2013 I've been wrestling with the idea of having a Web API project and a separate web site project in a single solution. My Web API 2 project opens up as: localhost:2020/ExampleProject.API My MVC 5 WebSite project opens up as: localhost:5050/ExampleProject.WebSite Now by default web api doesn't allow cross origin policies. So I played around with enabling CORS in my Web API 2, although I was able to get it to work, it only works for the latest browsers; I need the backward compatibility of IE7 to IE9. So I played around with

405 Method Not Allowed Web API 2

醉酒当歌 提交于 2019-12-05 10:24:45
I've been through just about every article I can find, on SO and more. I've made changes to the Web.config to meet the answers as they all seem to point to removing the WebDAV module and handler. However, I'm still getting the error: 405 Method Not Allowed The requested resource does not support http method 'PUT'. NOTE: this was originally just an MVC 4 project. I've added artifacts to support Web API. Seems like it's possible I may have missed something. NOTE: the GET calls are working just fine from Angular. Web API Route Config configuration.Routes.MapHttpRoute( name: "Default API",

No MediaTypeFormatter is available to read an object of type 'Advertisement' in asp.net web api

戏子无情 提交于 2019-12-05 10:20:05
问题 I have a class that name is Advertisement: public class Advertisement { public string Title { get; set; } public string Desc { get; set; } } and in my controller: public class OrderController : ApiController { public UserManager<IdentityUser> UserManager { get; private set; } // Post api/Order/Test [Route("Test")] public IHttpActionResult Test(Advertisement advertisement) { var currentUser = User.Identity.GetUserId(); Task<IdentityUser> user = UserManager.FindByIdAsync(currentUser); return Ok