'HttpContext' class 怎么用?
How do you use 'HttpContext' class?
我在使用 HttpContext class 时遇到问题。
我可以访问 HttpContext 但不能调用它的函数。
我想在助手中使用 HttpContext.Authentication class 我正在写。
我不能在我的助手中写这样的东西(见下文)class:
private void SignInUser()
{
... Code
await HttpContext.Authentication.SignInAsync(..);
}
但是,我可以在我的控制器中访问 HttpContext class。
如果有任何帮助,我将不胜感激。提前致谢。
首先创建文件HttpContext.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace System.Web
{
public static class HttpContext
{
private static IHttpContextAccessor _contextAccessor;
public static Microsoft.AspNetCore.Http.HttpContext Current => _contextAccessor.HttpContext;
internal static void Configure(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
}
public static class StaticHttpContextExtensions
{
public static void AddHttpContextAccessor(this IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app)
{
var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
System.Web.HttpContext.Configure(httpContextAccessor);
return app;
}
}
}
启动文件中的下一个
public void ConfigureServices(IServiceCollection services)
...
services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
services.AddHttpContextAccessor();
...
最后例如在控制器之外,如果您需要当前用户名
var currentUsername = !string.IsNullOrEmpty(System.Web.HttpContext.Current?.User?.Identity?.Name)
? System.Web.HttpContext.Current.User.Identity.Name
: "Anonymous";
我测试了 ASPNET Core 1.0 和 1.1
编辑
抱歉,我忘记在配置部分的启动文件中添加:
public void Configure(IApplicationBuilder app)
...
app.UseStaticHttpContext();
...
如果您想使用一个帮助程序 class 来登录用户,您应该创建一个扩展方法,它可以环绕 IHttpContextAccessor
或 HttpContext
本身。
public static class HttpContextExtensions
{
public static void SignInUser(this HttpContext context)
{
await context.Authentication.SignInAsync(..);
}
public static void SignInUser(this.IHttpContextAccessor contextAccessor)
{
// Calls the method from above
contextAccessor.HttpContext.SignInUser();
}
}
或者,如果您有更大的依赖链并需要其他注入服务,请将其转换为可注入服务 class:
public class UserLogonService : IUserLogonService
{
private readonly IHttpContextAccessor contextAccessor;
public UserLogonService(IHttpContextAccessor contextAccessor)
{
if(contextAccessor==null)
throw new ArgumentNullException(nameof(contextAccessor));
this.contextAccessor = contextAccessor;
}
public void SingInUser()
{
contextAccessor.HttpContext.SignInUser();
}
}
在Startup.Configure
方法中:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IUserLogonService, UserLogonService>();
现在您可以通过构造函数在项目的任何地方注入 IUserLogonService
,即
public class MyController
{
private readonly IUserLogonService userLogon;
public MyController(IUserLoggonService userLogon)
{
this.userLogon = userLogon;
}
public async Task<IActionResult> Login()
{
await userLogon.SignInUser();
}
}
它干净、解耦并且可以轻松进行单元测试,这是静态助手无法做到的 class。
我在使用 HttpContext class 时遇到问题。 我可以访问 HttpContext 但不能调用它的函数。
我想在助手中使用 HttpContext.Authentication class 我正在写。
我不能在我的助手中写这样的东西(见下文)class:
private void SignInUser()
{
... Code
await HttpContext.Authentication.SignInAsync(..);
}
但是,我可以在我的控制器中访问 HttpContext class。
如果有任何帮助,我将不胜感激。提前致谢。
首先创建文件HttpContext.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace System.Web
{
public static class HttpContext
{
private static IHttpContextAccessor _contextAccessor;
public static Microsoft.AspNetCore.Http.HttpContext Current => _contextAccessor.HttpContext;
internal static void Configure(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
}
public static class StaticHttpContextExtensions
{
public static void AddHttpContextAccessor(this IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
public static IApplicationBuilder UseStaticHttpContext(this IApplicationBuilder app)
{
var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
System.Web.HttpContext.Configure(httpContextAccessor);
return app;
}
}
}
启动文件中的下一个
public void ConfigureServices(IServiceCollection services)
...
services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
services.AddHttpContextAccessor();
...
最后例如在控制器之外,如果您需要当前用户名
var currentUsername = !string.IsNullOrEmpty(System.Web.HttpContext.Current?.User?.Identity?.Name)
? System.Web.HttpContext.Current.User.Identity.Name
: "Anonymous";
我测试了 ASPNET Core 1.0 和 1.1
编辑 抱歉,我忘记在配置部分的启动文件中添加:
public void Configure(IApplicationBuilder app)
...
app.UseStaticHttpContext();
...
如果您想使用一个帮助程序 class 来登录用户,您应该创建一个扩展方法,它可以环绕 IHttpContextAccessor
或 HttpContext
本身。
public static class HttpContextExtensions
{
public static void SignInUser(this HttpContext context)
{
await context.Authentication.SignInAsync(..);
}
public static void SignInUser(this.IHttpContextAccessor contextAccessor)
{
// Calls the method from above
contextAccessor.HttpContext.SignInUser();
}
}
或者,如果您有更大的依赖链并需要其他注入服务,请将其转换为可注入服务 class:
public class UserLogonService : IUserLogonService
{
private readonly IHttpContextAccessor contextAccessor;
public UserLogonService(IHttpContextAccessor contextAccessor)
{
if(contextAccessor==null)
throw new ArgumentNullException(nameof(contextAccessor));
this.contextAccessor = contextAccessor;
}
public void SingInUser()
{
contextAccessor.HttpContext.SignInUser();
}
}
在Startup.Configure
方法中:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IUserLogonService, UserLogonService>();
现在您可以通过构造函数在项目的任何地方注入 IUserLogonService
,即
public class MyController
{
private readonly IUserLogonService userLogon;
public MyController(IUserLoggonService userLogon)
{
this.userLogon = userLogon;
}
public async Task<IActionResult> Login()
{
await userLogon.SignInUser();
}
}
它干净、解耦并且可以轻松进行单元测试,这是静态助手无法做到的 class。