httpcontext

How can I make HttpContext available to be used by my Unit Tests?

谁说我不能喝 提交于 2019-12-01 02:33:50
问题 I want to write a unit test which tests the function of a class called UploadedFile. The problem I face is this class' static constructor uses HttpContext.Current property and because I am running my unit test from a class library I do not have an HttpContext at the testing time. Look at my static constructor: static UploadedFile() { if (HttpContext.Current == null) throw new Exception("web server not available"); HttpServerUtility server = HttpContext.Current.Server; // SET

HttpContext not available in Class Library

你说的曾经没有我的故事 提交于 2019-12-01 02:10:47
I am working on a project where I have a C# class library which needs to use the System.web.HttpContext . I've done this before in another project without problem but now its not working. I'm not sure what I am missing they're both targeting .net 3.5 and I've added the reference System.web and added the directive using System.web . However, when I try and do HttpContext nothing is found. I've tried using the full path System.web.HttpContext but the only thing that comes up are 3 items related to ASP. Below are screenshots from the working project intellisense and the non-working intellisense

How to mock httpcontext so that it is not null from a unit test?

我只是一个虾纸丫 提交于 2019-11-30 21:36:10
I am writing a unit test and the controller method is throwing an exception because HttpContext / ControllerContext is null. I don't need to assert anything from the HttpContext, just need it to be not NULL. I have done research and I believe Moq is the answer. But all the samples that I have seen haven't helped me a lot. I don't need to do anything fancy, just to mock the httpcontext. Point me in the right direction! TheFlyingCircus Got these two functions from here in a class; public static class HttpContextFactory { public static void SetFakeAuthenticatedControllerContext(this Controller

Does static reference to HttpContext.Current.Session return same session for all users?

偶尔善良 提交于 2019-11-30 21:08:56
Is there room for issue in the following code in terms of multiple users of the same web application? I mean, I know that a purely static string will be shared across all sessions for a single ASP.NET application, but given that this explicitly refers to the Current.Session , even though it is static it seems like it would always refer to the session instance of the "current user." But an error is happening that could be explained by everyone sharing the current value of Mode and thus the most recent change overwriting everyone else's mode value. (As a background: This string is in a Helpers

How do I access HttpContext in Server-side Blazor?

被刻印的时光 ゝ 提交于 2019-11-30 17:01:46
问题 I need to access HttpContext in a page (.cshtml) and in particular a request and then a cookie. Despite available, HttpContextAccessor always has a null value stored in its HttpContext property. Any ideas would be much appreciated. Thanks in advance. EDIT: the Blazor version I use is: 0.7.0. 回答1: Add the following to Blazor.Web.App.Startup.cs: services.AddHttpContextAccessor(); You also need this in <component-name>.cshtml @using Microsoft.AspNetCore.Http @inject IHttpContextAccessor

HttpContext.Current null inside async task

寵の児 提交于 2019-11-30 12:18:28
I have a method that uses a repository ( userRepo ): public override Task<IdentityResult> CreateLocalUserAsync(IUser user, string password, CancellationToken cancellationToken) { var task = new Task<IdentityResult>(() => { TUserEntity newUser = new TUserEntity { Id = user.Id, UserName = user.UserName, Password = password }; userRepo.Save(newUser).Flush(); return new IdentityResult(true); }, cancellationToken); task.Start(); return task; } The userRepo object has a dependency that uses HttpContext.Current . Both of these are resolved using ninject InRequestScope . The above method is called

Unified static class between HttpContext and SignalR HubCallerContext

痞子三分冷 提交于 2019-11-30 11:32:25
I have a lot of code that depends on HttpContext.Current, and I noticed that requests that come from SignalR hubs have HttpContext.Current == null , so my code breaks, for example: HttpContext.Current.Request.IsAuthenticated So I came up with following: public static class UnifiedHttpContext { private static HubCallerContext SignalRContext { get; set; } private static int SignalRUserId { get { return WebSecurity.GetUserId(SignalRContext.User.Identity.Name); } } private static bool IsSignalRRequest { get { return SignalRContext != null; } } public static void SetSignalRContext(HubCallerContext

StructureMap is not disposing data context when using HttpContextScoped()

不想你离开。 提交于 2019-11-30 08:58:33
My goal is to have one data context ( MainDbContext ) per HTTP request in ASP.NET MVC and dispose the data context when the request ends. I'm using the following StructureMap configuration: public static class ContainerConfigurer { public static void Configure() { ObjectFactory.Initialize(x => { x.For<MainDbContext>().HttpContextScoped(); }); } } Whenever I need a MainDbContext , I'm using this code: var dbContext = ObjectFactory.GetInstance<MainDbContext>(); This is working as expected: only one data context is being created per HTTP request. The problem is, MainDbContext is not being

Entity Framework Object Context in ASP.NET Session object?

大城市里の小女人 提交于 2019-11-30 07:38:46
We have a multi-layered Asp.NET Web Forms application. The data layer has a class called DataAccess which impements IDisposable and has an instance of our Entity Framework Object Context as a private field. The class has a number of public methods returning various collections of Entities and will dispose its Object Context when it is disposed. Due to a number of problems we've been facing, we decided it would be a big plus to keep the Object Context (or an instance of DataAccess ) in scope for longer on the server. A suggestion was made to keep an instance in the HttpContext.Current.Items

How does HttpContext.Current.User.Identity.Name know which usernames exist?

非 Y 不嫁゛ 提交于 2019-11-30 06:23:20
问题 This is not necessarily an issue, I am just curious as to how it works. I have a method: public static bool UserIsAuthenticated() { bool isAuthed = false; try { if (HttpContext.Current.User.Identity.Name != null) { if (HttpContext.Current.User.Identity.Name.Length != 0) { FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; FormsAuthenticationTicket ticket = id.Ticket; isAuthed = true; string MyUserData = ticket.UserData; } } } catch { } // not authed return isAuthed; } The