httpcontext

Passing HttpContext.Current.User.Identity to WCF

a 夏天 提交于 2019-12-05 07:34:47
Looking for a little advice (or maybe even a direct answer). I have an MVC3 website. I also have a set of WCF services running (for now everything is on the same box). What I'm trying to do is authenticate the client (that part is working fine), then pass that authenticated user on to various WCF calls. At the moment I've hooked up the Application_AuthenticateRequest() method in Global.Asax , which boils down to creating a new GenericIdentity & GenericPrincipal , then assigning that principal to HttpContext.Current.User : ... GenericIdentity identity = new GenericIdentity(userName);

Mock ServerVariables in the HttpContext.Current.Request

余生长醉 提交于 2019-12-05 05:24:34
One of my services uses server variable provided by IIS through such code var value = System.Web.HttpContext.Current.Request.ServerVariables["MY_CUSTOM_VAR"]; What I've tried is to mock those object and insert my own variable/collection and check few cases(e.g. variable is missing, value is null ...) I'm able to create instances of HttpContext, HttpRequest, HttpResponse and assign them properly however each of them is just a plain class without interface or virtual properties and initialization of ServerVariables happens somewhere under the hood. HttpContext mocking: var httpRequest = new

Convert HttpContent into byte[]

Deadly 提交于 2019-12-05 03:35:05
I am currently working on a c# web API. For a specific call I need to send 2 images using an ajax call to the API, so that the API can save them as varbinary(max) in the database. How do you extract an Image or byte[] from a HttpContent object? How do I do this twice? Once for each image. - var authToken = $("#AuthToken").val(); var formData = new FormData($('form')[0]); debugger; $.ajax({ url: "/api/obj/Create/", headers: { "Authorization-Token": authToken }, type: 'POST', xhr: function () { var myXhr = $.ajaxSettings.xhr(); return myXhr; }, data: formData, cache: false, contentType: false,

Accessing HttpContext and User Identity from data layer

倾然丶 夕夏残阳落幕 提交于 2019-12-04 21:41:23
问题 I need to implement AddedBy/ChangedBy type fields on my Base Entity that all other entities inherit from ( Fluent Nhibernate ). Accessing HttpContext.User.Identity from within my Repository/Data layer is probably not a good idea.. or is it ? What's the best way to grab my user ( current identity ) information to record who the records were added or changed by ? Re-factoring the entire application to include user information in repository calls would be silly. I'm sure there is a better, more

Can I fool HttpRequest.Current.Request.IsLocal?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 19:38:22
问题 I'm running a web application that displays some debugging behavior if it's being run locally - quotes around resource strings, etc - and I'd like to demo the application on my laptop at a conference where I won't have internet access, so it has to be local. The application uses HttpContext.Current.Request.IsLocal to determine if it's running locally - is there any way to fool it? I'd like to trick it into returning "False" even though I am indeed running locally. I do have access to the

HttpContext Class and its Thread Safety

左心房为你撑大大i 提交于 2019-12-04 18:00:44
I have an Singleton object in application that has following property: private AllocationActionsCollection AllocationActions { get { return HttpContext.Current.Session["AllocationOptions.AllocationActions"] as AllocationActionsCollection; } set { HttpContext.Current.Session["AllocationOptions.AllocationActions"] = value; } } I'm dealing with one error ( HttpContext.Current.Session["AllocationOptions.AllocationActions"] is null even though it is supposed to me always set to valid instance...). I just read in MSDN that HttpContext instance member are not guaranteed to be thread safe! I wonder if

Using httpcontext in unit test

霸气de小男生 提交于 2019-12-04 17:03:46
问题 I'm using C#4.0 and i need to unit test a service. The function inside the service returns a path similar to the variable i called expected, this is the path i'm expecting to get back. But when i run this test i'm getting the error that HttpContext.Current is NULL. What can i do to fix this issue so the test can be ran? [TestMethod] public void GetPathTest() { var expected = System.IO.Path.GetFullPath(HttpContext.Current.Server.MapPath("~/Certificates/")); var path = _mockService.Setup(o => o

How can I use System.Web.Caching.Cache in a Console application?

主宰稳场 提交于 2019-12-04 08:04:00
问题 Context: .Net 3.5, C# I'd like to have caching mechanism in my Console application. Instead of re-inventing the wheel, I'd like to use System.Web.Caching.Cache (and that's a final decision, I can't use other caching framework, don't ask why). However, it looks like System.Web.Caching.Cache is supposed to run only in a valid HTTP context. My very simple snippet looks like this: using System; using System.Web.Caching; using System.Web; Cache c = new Cache(); try { c.Insert("a", 123); } catch

Web API Service - How to use “HttpContext.Current” inside async task

廉价感情. 提交于 2019-12-04 03:25:52
I'm using a "Post" async method of webApi rest service: public async Task<object> Post([FromBody]string data) { object response = ExecuteServerLogics(data); return response; } This above code worked good but in some of the client's calls, we experienced performance issues. After reading some articles here, i've noticed that our webApi rest service, is not really working asynchronously with its incoming web requests, because we forgot to use async/await pattern : public async Task<object> Post([FromBody]string data) { object response = await Task<object>.Run( () => { return ExecuteServerLogics

adding header to http response in an action inside a controller in asp.net/mvc

て烟熏妆下的殇ゞ 提交于 2019-12-04 02:39:39
I am streaming data from server to client for download using filestream.write . In that case what is happening is that I am able to download the file but it does not appear as download in my browser. Neither the pop-up for "Save As" appears not "Download Bar" appears in Downloads section. From looking around, I guess I need to include "something" in the response header to tell the browser that there is an attachment with this response. Also I want to set the cookie. To accomplish this, this is what I am doing: [HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment