asp.net-web-api2

how can i call web api Controller from ajax

丶灬走出姿态 提交于 2019-12-10 14:23:42
问题 I am a beginner of ASP MVC with web api. By using below codes I tried to call a function which is written at the controller. For checking I used breakpoint so the control could not go to the controller so that I can not track actually what is going on. The given code explains how to pass username and password to the controller: <script type="text/javascript"> function Login() { var Login = {}; Login.username = document.getElementById("txtUserName").value; Login.password = document

Web API 2 and ASP Identity - Handling of locked out users

我是研究僧i 提交于 2019-12-10 13:47:47
问题 I just migrated my web app (ASP.NET MVC) to ASP Identity. Everything works fine after quite some work, except the API which the web app provides. This is a WEB API 2, and it is using the bearer token mechanism to authenticate users. The authentication itself also works fine. but: When a user is locked out, the token for the user is still issued via the API-token-endpoint. Is there a suggested way to handle this? I did not find any example... Thanks! 回答1: Ok, that was a stupid one... I see

How to consume JWT access token and user claims using RestSharp

孤街醉人 提交于 2019-12-10 13:46:24
问题 I'm using below code to consume JWT access token from an Asp.net Web Api 2.2 service. I have followed this article to setup an authorization server in Web Api service. I'm using RestSharp in the client. client code: var client = new RestClient(http://localhost:58030); client.Timeout = 30000; var request = new RestRequest(@"/Oauth/Token", Method.POST); request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddHeader("grant_type", "password"); request.AddHeader(

HttpRequestBase.GetBufferedInputStream not found when doing a WebAPI 2 JSON Post

喜你入骨 提交于 2019-12-10 13:45:11
问题 Created a MVC5 WebAPI2 project using Visual Studio Created a basic JSON POST Post arguments cause HttpRequestBase.GetBufferedInputStream not found failure Works on: Windows with visual studio Fails on: OSX Xamarin Gentoo Xamarin Test application: Mono MVC5 Web API2 Test Case Test procedure: run the MVC application in xamarin On load the page will do a ajax post to the server. a) server will return a 500 error on failure b) console.log post arg on success Error: { "Message": "An error has

Asp.net 5 web api return status code and body

对着背影说爱祢 提交于 2019-12-10 13:41:53
问题 I'm working on a project using ASP.NET 5 and I'm writing a web api. I've inherited some code and database stored procedures that use raiserror to indicate something is wrong (username/password incorrect, expired license etc). The stored procedure returns nothing to uniquely identify that error, except the message text. I want to be able to return a HTTP UNAUTHORIZED response, but also shuttle the error message along to the client too. The built in IActionResult HttpUnauthorized() method doesn

Get Url of current ASP.NET Web Api 2 action

微笑、不失礼 提交于 2019-12-10 12:49:11
问题 In ASP.NET Web API 2, how can I get Url of current action. Following is illustrative example. [HttpGet] [Route("api/someAction")] public SomeResults GetAll() { var url = /* what to write here*/ .... } 回答1: One of the properties of the ApiController base class (from which your own controller must be derived) is called Request : // Summary: // Defines properties and methods for API controller. public abstract class ApiController : IHttpController, IDisposable { // ... // // Summary: // Gets or

Failure to Include system.web.http in unit tests?

感情迁移 提交于 2019-12-10 11:48:24
问题 I'm currently trying to unit test an ApiController I've created. What I'm stumbling over is that for some reason I can't "use" System.Web.Http . In my main project though I can use the using without any problem (and thus use IHttpActionResult ). Now what I'm not getting is why is it not possible in the unit test Project? Am I overlooking here something or do I need to do something differently? 回答1: As suggested here, System.Web.Http can be installed via NuGet Get-Project MyTestProject |

Angular 2 - Consuming restful api calls with windows authentication

僤鯓⒐⒋嵵緔 提交于 2019-12-10 11:13:02
问题 I have a .net web api hosted on IIS 7 on a remote server which uses windows authentication. I want to access the web api using Angular 2 using TypeScript with node . Earlier i was getting an error 'Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource' I added this on the hosted Application's web.config <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> But

Best way to convert Asp.Net MVC controllers to Web API

感情迁移 提交于 2019-12-10 11:06:21
问题 I have this ASP.NET MVC 5 project which I'm converting over to AngularJS with MS Web Api. Now in the old project I have these c# controllers of type Controller , however in my new project I've created some new Web Api controllers of type ApiController . Now I'd like to reuse the old controller code in my new project. Herein lies my confusion. As I attempt to port the old controller code over to my Web Api controller, I'm getting some front-end $http request errors. Here's a function from my

UserManager dependency injection with Owin and Simple Injector

隐身守侯 提交于 2019-12-10 10:58:42
问题 After adding Simple Injector to my project to create an instance of my UserAppManager that will exist through the whole lifetime of a session I started to recieve errors: Error with no parameterless constructor: An error occurred when trying to create a controller of type 'AuthController'. Make sure that the controller has a parameterless public constructor. Error with a parameterless constructor: For the container to be able to create AuthController it should have only one public constructor