asp.net-web-api2

ASP.NET Web API 2 - How to implement OAuth2.0 for SPA applications

懵懂的女人 提交于 2020-01-06 07:07:18
问题 I am trying to implement WEB API 2 (for Single Page App, not as part of Visual Studio project) OAuth2.0 protocol. As per Which OAuth 2.0 flow should I use, using refresh tokens is not an option. However, I am not sure I understand Implicit Grant flow with eventual Silent Authentication. Does Implicit Flow mean only issuing normal access tokens? In that case, how do we allow user to stay logged in for long time? How should Silent Authentication endpoint look like, what should it receive and

ASP.NET Web API Controller output is always buffered

人盡茶涼 提交于 2020-01-06 06:54:06
问题 How to send output without buffering? I defined my API controller this way: public class DefaultController : ApiController { [HttpGet] [Route] public HttpResponseMessage Get() { var response = Request.CreateResponse(); response.Content = new PushStreamContent( (output, content, context) => { using (var writer = new StreamWriter(output)) { for (int i = 0; i < 5; i++) { writer.WriteLine("Eh?"); writer.Flush(); Thread.Sleep(2000); } } }, "text/plain"); return response; } } Output appears in the

Multiple IHttpActionResults GET's within a single ApiController fail

假装没事ソ 提交于 2020-01-05 23:05:53
问题 I am playing with WebApi2 and came across an odd issue. I have updated the default ValuesController to use IHttpActionResult like public class ValuesController : ApiController { // GET api/values [HttpGet] public IHttpActionResult Get() { return Ok(new string[] { "value1", "value2" }); } // GET api/values/get2 [HttpGet] public IHttpActionResult Get2() { return Ok(new string[] { "value1", "value2" }); } When I try call Get() within postman I get an error { "Message": "An error has occurred.",

Breeze ODataException without $expand option

徘徊边缘 提交于 2020-01-05 17:45:15
问题 We are two developers (one with Windows Server 2008 R2 and the other with Windows 8) that working on same project (on TFS with Visual Studio 2013 and with the last nuget packages like Breeze 1.4.11 installed), but with different results on breeze query execution. With Windows Server 2008 R2 we must add $expand option otherwise we have an error Only properties specified in $expand can be traversed in $select query options , conversely with Windows 8 the query run successfully without $expand

How to deal with null HttpContext.Current when using auth filters with async/await?

那年仲夏 提交于 2020-01-05 14:53:40
问题 In one of apis in my controller I need to reference HttpContext.Current. Everything worked fine until I implemented a custom authentication filter. The filter uses async methods with await. However, with the filter in place, the HttpContext.Current is null when it reaches my controller. I'm assuming that this is because my controller executes on a different thread that it used to due to async/await in the filter. If this is the case, how should I access the 'original' context in my controller

How to deal with null HttpContext.Current when using auth filters with async/await?

断了今生、忘了曾经 提交于 2020-01-05 14:53:37
问题 In one of apis in my controller I need to reference HttpContext.Current. Everything worked fine until I implemented a custom authentication filter. The filter uses async methods with await. However, with the filter in place, the HttpContext.Current is null when it reaches my controller. I'm assuming that this is because my controller executes on a different thread that it used to due to async/await in the filter. If this is the case, how should I access the 'original' context in my controller

How to deal with null HttpContext.Current when using auth filters with async/await?

让人想犯罪 __ 提交于 2020-01-05 14:53:03
问题 In one of apis in my controller I need to reference HttpContext.Current. Everything worked fine until I implemented a custom authentication filter. The filter uses async methods with await. However, with the filter in place, the HttpContext.Current is null when it reaches my controller. I'm assuming that this is because my controller executes on a different thread that it used to due to async/await in the filter. If this is the case, how should I access the 'original' context in my controller

Returning binary data with web api

时光总嘲笑我的痴心妄想 提交于 2020-01-05 11:56:21
问题 I have the following controller method which returns a byte array. public async Task<HttpResponseMessage> Get() { var model = new byte[] { 1, 2, 3 }; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(new MemoryStream(model)); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return result; } I think this is an older way of implementing this functionality with web api. Is there a more "modern"

Async Web Service call not consistently asynchronous

巧了我就是萌 提交于 2020-01-05 08:28:09
问题 I'm having issues creating an asynchronous web service using the Task Parallel Library with ASP.NET Web API 2. I make an asynchronous call to a method StartAsyncTest and create a cancellation token to abort the method. I store the token globally and then retrieve it and call it from a second method CancelAsyncTest . Here is the code: // Private Global Dictionary to hold text search tokens private static Dictionary<string, CancellationTokenSource> TextSearchLookup = new Dictionary<string,

How to send bearer token to views in ASP NET MVC 5?

廉价感情. 提交于 2020-01-05 04:41:31
问题 I have a .NET MVC and WEB API project. I want to call the WEB API controllers from javascript but I didn't find a way to send the token to my views. I want to add the bearer token in Viewbag variable, using the below code: protected override void OnActionExecuting(ActionExecutingContext filterContext) { //GetBearerToken doesn't exists ViewBag.BearerToken = Request.GetOwinContext().GetBearerToken(); } In _Layout.cshtml , I added the folowing code to set Authorization header for all ajax