httpcontext

HttpContext

雨燕双飞 提交于 2021-02-19 04:20:19
HttpContext.Current并非无处不在 阅读目录 开始 无处不在的HttpContext HttpContext.Current到底保存在哪里? HttpContext并非无处不在! 如何获取文件绝对路径? 异步调用中如何访问HttpContext? 安全地使用HttpContext.Current 了解ASP.NET的开发人员都知道它有个非常强大的对象 HttpContext,而且为了方便,ASP.NET还为它提供了一个静态属性HttpContext.Current来访问它, 今天的博客打算就从HttpContext.Current说起。 回到顶部 无处不在的HttpContext 由于ASP.NET提供了静态属性HttpContext.Current,因此获取HttpContext对象就非常方便了。 也正是因为这个原因,所以我们经常能见到直接访问HttpContext.Current的代码: public class Class1{ public Class1() { string file = HttpContext.Current.Request.MapPath("~/App_Data/xxxxxx.xml"); string text = System.IO.File.ReadAllText(file); //..........其它的操作 } //

Is there any difference between HttpContext.Current.Request and HttpContext.Request?

我怕爱的太早我们不能终老 提交于 2021-02-18 08:38:52
问题 If I want to access Request object in controller action I need to write HttpContext.Request , whereas if I want to access the same object in MVC view, I need to write HttpContext.Current.Request . Is there any difference between them? The problem I am facing is that, the cookies which I set through HttpContext.Response.Cookies.Add in controller action are not being retrieved in HttpContext.Current.Request.Cookies collection in an MVC view, though I can see those cookies through javascript.

IHttpContextAccessor.HttpContext.User.Identity shows all null properties in CurrentUserService service

谁都会走 提交于 2021-02-15 04:57:10
问题 I am trying to use Jason Taylor's Clean Architecture Template, this template uses NSwag to automatically create a TypeScript Client (Angular), but I don't need to create a TS client, so my main goal is to replace it with Razor Pages. I've been able to achieve this to a high grade, but I'm having trouble with the CurrentUserService whenever it gets instantiated it is supposed to set the UserId in this line: UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes

IHttpContextAccessor.HttpContext.User.Identity shows all null properties in CurrentUserService service

我的梦境 提交于 2021-02-15 04:54:09
问题 I am trying to use Jason Taylor's Clean Architecture Template, this template uses NSwag to automatically create a TypeScript Client (Angular), but I don't need to create a TS client, so my main goal is to replace it with Razor Pages. I've been able to achieve this to a high grade, but I'm having trouble with the CurrentUserService whenever it gets instantiated it is supposed to set the UserId in this line: UserId = httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes

Manually creating an HttpContext in ASP.NET Core 2.x

风格不统一 提交于 2021-02-07 14:46:25
问题 I'm trying to render a Razor view to a string from a Hosted Service. By using the IRazorViewEngine I am able to render a view to a string using something like the following: _viewEngine.FindView(actionContext, viewName, false); var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }; var viewContext = new ViewContext( actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext,

Manually creating an HttpContext in ASP.NET Core 2.x

删除回忆录丶 提交于 2021-02-07 14:43:38
问题 I'm trying to render a Razor view to a string from a Hosted Service. By using the IRazorViewEngine I am able to render a view to a string using something like the following: _viewEngine.FindView(actionContext, viewName, false); var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }; var viewContext = new ViewContext( actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext,

Why should I inject IHttpContextAccessor as a Singleton

瘦欲@ 提交于 2021-02-07 11:27:17
问题 In all examples that i've seen of IHttpContextAccessor injection, it is set as a Singleton. Examples: How to add IHttpContextAccessor in the Startup class in the DI in ASP.NET Core 1.0? Injecting IHttpContextAccessor into ApplicationDbContext ASP.NET Core 1.0 .NET Core IHttpContextAccessor issue I find it kinda weird, since HttpContext really looks like something that maps to requests. Wouldn't AddScoped be more appropriate in this case? Is Singleton really the recomended way? Am I not

ASP.NET MVC: How to use HttpContext.User

旧城冷巷雨未停 提交于 2021-02-04 15:53:06
问题 Im getting really lost on how to use HttpContext.User. I read everywhere that its great for FormAutherication, but i just cant see how it works. If i do something like this: ControllerContext.HttpContext.User = new GenericPrincipal(GetUser(username, password), roles); What does ControllerContext.HttpContext.User contain? and how do i access information about the user this way? Im think that i have a Action like this: public User GetUser(string username, string password) { try { var user =

Caching objects on httpcontext asp.net core

我是研究僧i 提交于 2021-01-28 10:08:32
问题 I'm trying to migrate from .net framework MVC 5 to .net core 2.0 MVC good old times I can get or set the cached objects by calling HttpContext.Current.Application.Lock(); HttpContext.Current.Application["foo"] = bar; HttpContext.Current.Application.Lock(); but I cant migrated this part of my application to the .net core. How can I handle this on core 2.0? 回答1: HttpContext.Current.Application was a feature that was left in to make applications easier to port over from ASP classic. It has been

User.Identity.Name is null after federated Azure AD login with aspnetcore 2.2

元气小坏坏 提交于 2020-12-25 04:48:06
问题 I've followed AzureAD aspnetcore sample as closely as possible to try and implement Azure AD authentication in our aspnetcore 2.2 webapp. I am able to login successfully using Azure AD. However, the user's name is not being displayed AFTER login. https://github.com/Azure-Samples/active-directory-b2c-dotnetcore-webapp This value should be read in the view from the User.Identity.Name property. On further inspection, I can see that the principal claims are being correctly returned to the