C# 如何在逻辑层访问请求实体
我想将此服务转移到logic,以便在任何地方使用,但我无法成功,因为它来自控制器 我有两种服务。有读缓存,我在验证时在控制器层使用它们 我的第一个逻辑是在缓存中读取companyIdC# 如何在逻辑层访问请求实体,c#,asp.net,.net,asp.net-core,asp.net-web-api,C#,Asp.net,.net,Asp.net Core,Asp.net Web Api,我想将此服务转移到logic,以便在任何地方使用,但我无法成功,因为它来自控制器 我有两种服务。有读缓存,我在验证时在控制器层使用它们 我的第一个逻辑是在缓存中读取companyId public virtual int GetCompanyIdFromCache(int id) { _memCache.TryGetValue(id, out int companyId); return companyId; } 我的第二个服务也在控制
public virtual int GetCompanyIdFromCache(int id)
{
_memCache.TryGetValue(id, out int companyId);
return companyId;
}
我的第二个服务也在控制器上。(帮助我查找用户的id)
我想在任何地方都使用这种方法,所以我想将第二个服务完全移动到逻辑层,但用户字段来自ControllerBase(我猜是在HttpContext上),我无法将其移动到逻辑层
if (User.Identity is ClaimsIdentity claimsIdentity)
我应该如何重构逻辑层?据我所知,User.Identity是ClaimsIdentity,它是controllerbase中的一个属性。我们不能直接在其他方法中使用它 如果您想在其他服务方法中访问User.Identity,我建议您可以尝试注入httpaccessor服务并从中获取索赔实体 更多详细信息,请参考以下代码: 创建myclass:
public class Myclass
{
public IHttpContextAccessor _accessor { get; set; }
public Myclass(IHttpContextAccessor accessor)
{
_accessor = accessor;
var re = accessor.HttpContext.User.Identity as ClaimsIdentity;
int i = 0;
}
public string GetName() {
var re = _accessor.HttpContext.User.Identity as ClaimsIdentity;
string name = re.Claims.First(x => x.Type == "name").Value;
return name;
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddHttpContextAccessor();
services.AddScoped(typeof(Myclass));
}
用法:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public Myclass test { get; set; }
public HomeController(ILogger<HomeController> logger, Myclass _test)
{
_logger = logger;
test = _test;
}
public async Task<IActionResult> IndexAsync()
{
var claimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
claimsIdentity.AddClaim(new Claim("name", "aaaa"));
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity)
);
return View();
}
public async Task<IActionResult> PrivacyAsync()
{
var re= test.GetName();
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
公共类HomeController:控制器
{
专用只读ILogger\u记录器;
公共Myclass测试{get;set;}
公共家庭控制器(ILogger记录器,Myclass\u测试)
{
_记录器=记录器;
测试=_测试;
}
公共异步任务IndexAsync()
{
var claimsIdentity=新的claimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
补充索赔(新索赔(“名称”、“aaaa”));
等待HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
新索赔(索赔实体)
);
返回视图();
}
公共异步任务privacySync()
{
var re=test.GetName();
返回视图();
}
[ResponseCache(持续时间=0,位置=ResponseCache位置。无,NoStore=true)]
公共IActionResult错误()
{
返回视图(新的ErrorViewModel{RequestId=Activity.Current?.Id??HttpContext.TraceIdentifier});
}
}
结果:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public Myclass test { get; set; }
public HomeController(ILogger<HomeController> logger, Myclass _test)
{
_logger = logger;
test = _test;
}
public async Task<IActionResult> IndexAsync()
{
var claimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
claimsIdentity.AddClaim(new Claim("name", "aaaa"));
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity)
);
return View();
}
public async Task<IActionResult> PrivacyAsync()
{
var re= test.GetName();
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}