asp.net-web-api

How to read headers data in hub SignalR ASP.NET Core 2.1

天涯浪子 提交于 2021-02-20 09:47:26
问题 I'm trying to pass userId to hub on connection to signalR. This is how client sets up the connection: connection = new HubConnectionBuilder() .WithUrl("http://localhost:56587/hub", options => { options.Headers["UserId"] = loginTextBox.Text; }) .AddMessagePackProtocol() .Build(); How can I read this header in OnConnectedAsync() method in my hub? 回答1: To get Header Value as string: public override async Task OnConnectedAsync() { var httpCtx = Context.GetHttpContext(); var someHeaderValue =

How to read headers data in hub SignalR ASP.NET Core 2.1

折月煮酒 提交于 2021-02-20 09:47:00
问题 I'm trying to pass userId to hub on connection to signalR. This is how client sets up the connection: connection = new HubConnectionBuilder() .WithUrl("http://localhost:56587/hub", options => { options.Headers["UserId"] = loginTextBox.Text; }) .AddMessagePackProtocol() .Build(); How can I read this header in OnConnectedAsync() method in my hub? 回答1: To get Header Value as string: public override async Task OnConnectedAsync() { var httpCtx = Context.GetHttpContext(); var someHeaderValue =

How do I access URL parameters in web API?

柔情痞子 提交于 2021-02-20 04:14:15
问题 I am trying to create a REST service using the .NET Web API. The URL I am trying to map is /api/<controller>/<videoId>/chapters I have a route setup that looks like the following: RouteTable.Routes.MapHttpRoute(name: "Route1", routeTemplate: "api/video/{id}/{action}", defaults: new { controller = "Video", action = "Chapters"}); Which maps the following function in the controller: [HttpGet] [ActionName("Chapters")] public string GetChapters() { return "get chapters"; } Everything maps

oauth web api call returns 403 forbidden

牧云@^-^@ 提交于 2021-02-19 08:19:46
问题 I implemented OAuth2 in my mvc web api project.I got the token but when I call the web api methods with this token the call will return 403 forbidden.I also called the same web api without the token it's working.Here is my token call: public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { AccountLogin LogCredentials = new AccountLogin(); LogCredentials.UserName = context.UserName; LogCredentials.Password = context.Password; LogCredentials

Resource based ASP.NET Authorization FormsAuthentication and WebApi

烂漫一生 提交于 2021-02-19 08:01:28
问题 Using webapi with formsauthentication (i know may sound weird, but it exactly what I need). I have a route like : [Route("{application}/orders}"] public IHttpActionResult Get(string application) { var orders = OrderService.GetOrders(application); // return list of orders of applicaiton. return Ok(orders); } Example scenario : User John has permission to see orders for application "CoolApp" User Leia has permission to see orders for application "Robin" user Tristan doesn't have permission to

Client Authentication for WebAPI 2

倖福魔咒の 提交于 2021-02-19 07:39:25
问题 My company has written an API to expose our application data to our clients. We've completed the endpoints and now want to secure the API. The API will only be used by pre-approved clients so no anonymous access is needed. I've been told that we can use an x.509 certificate that we generate to identify and authenticate each client. By identifying, I mean embedding a client code in the certificate that we issue to each client (is this even possible?). As you can probably tell I have little

How to run self-hosted(owin based) web api on IIS?

一笑奈何 提交于 2021-02-19 06:50:34
问题 I have developed self-hosted(owin based) web api using console application. In development phase I was running the console application and everything was okay. static void Main(string[] args) { string baseUri = "http://+:8080"; Console.WriteLine("Starting web Server..."); var server = WebApp.Start<Startup>(baseUri); Console.WriteLine("Server running at {0} - press Enter to quit. ", baseUri); Console.ReadLine(); server.Dispose(); } Now I need to deploy my self-hosted web api to run on IIS.So,

Post DbGeography data type in odata v4 api

一世执手 提交于 2021-02-19 06:18:07
问题 What i receive from a DbGeography data type is in Location. { "@odata.context":"http://subdomain.mydomain.me/odata/$metadata#Pois","value":[ { "Id":"d57e6603-5530-4457-b041-6398b7182173","Name":"Demo Home","Address":"Zuidstraat 8 8630 Veurne","Location":{ "Geography":{ "CoordinateSystemId":4326,"WellKnownText":"POINT (51.515574 2.025285)","WellKnownBinary":null } },"TagId":"ec32e8f3-c0b8-4bcc-af6c-7059b1ef1a65","RouteId":null } ] } I have tried multiple POSTS, but none of them seem to succeed

Save response as file

ⅰ亾dé卋堺 提交于 2021-02-19 04:20:31
问题 I have WebAPI method which returns HttpResponseMessage with .csv file as Content: private static HttpResponseMessage FileAsAttachment(string file) { var now = DateTime.Now; var result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StringContent(file); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download result

hot-deployments with Self Host Web Api Services

早过忘川 提交于 2021-02-19 04:07:46
问题 Context: My company has been developing a WebAPI application to be hosted by IIS and the request latencies for a single static content file are about ~60ms. We investigated benchmarking the same app using WebAPI Self host and the latencies for the same content file were ~15ms, which really blew us away. From a deployment process, we love IIS as it provides us extreme flexibility in doing hot deployments by copying DLLs directly to our web servers, which doesn't require us to do any sort of