C# 控制器(asp web api)中的HttpContext(User.Identity)不工作
我在正确使用HttpContext.Current.User.Identity时遇到一些问题。从控制器构造函数来看,这不起作用,我必须将其实现到某个方法中。看看这个例子C# 控制器(asp web api)中的HttpContext(User.Identity)不工作,c#,authentication,asp.net-web-api,asp.net-identity,C#,Authentication,Asp.net Web Api,Asp.net Identity,我在正确使用HttpContext.Current.User.Identity时遇到一些问题。从控制器构造函数来看,这不起作用,我必须将其实现到某个方法中。看看这个例子 public class SomeControler : ApiController { private UserData userData; // NOT WORKING public ChartsController(
public class SomeControler : ApiController
{
private UserData userData;
// NOT WORKING
public ChartsController(
RegisteredUserData registeredUserData,
NotLoggedInUserData NotLoggedInUserData
{
var isAuthenticated = HttpContext.Current.User.Identity.IsAuthenticated;
this.userData = isAuthenticated
? (IUserData)registeredUserData
: (IUserData)NotLoggedInUserData;
}
// WORKING
public SomeMethod(
RegisteredUserData registeredUserData,
NotLoggedInUserData NotLoggedInUserData
{
var isAuthenticated = HttpContext.Current.User.Identity.IsAuthenticated;
this.userData = isAuthenticated
? (IUserData)registeredUserData
: (IUserData)NotLoggedInUserData;
}
}
我怎样才能解决这个问题?我花了很多时间在网上寻找答案,但我没有得到这个答案
问候
编辑
我找到了答案。这是个好办法吗
public class SomeControler : ApiController
{
private RegisteredUserData registeredUserData;
private NotLoggedInUserData notLoggedInUserData;
private UserData userData
{
get
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
return registeredUserData;
}
return notLoggedInUserData;
}
}
public ChartsController(
RegisteredUserData registeredUserData,
NotLoggedInUserData notLoggedInUserData
{
this.registeredUserData = registeredUserData;
this.notLoggedInUserData = notLoggedInUserData;
}
}
首先,请求和
HttpContext
在控制器的构造中还不可用,因为在请求流中控制器是在哪里初始化的。您必须在一个操作中访问它,到那时,请求和上下文将完全实现
接下来,不要将控制器耦合到HttpContext
。这使得您的代码难以测试和维护
在服务抽象中提取所需的信息
public interface IUserDataAccessor {
IUserData UserData { get; }
}
public class UserDataAccessor : IUserDataAccessor {
private readonly RegisteredUserData registeredUserData;
private readonly NotLoggedInUserData notLoggedInUserData;
public UserDataAccessor(
RegisteredUserData registeredUserData,
NotLoggedInUserData notLoggedInUserData) {
this.registeredUserData = registeredUserData;
this.notLoggedInUserData = notLoggedInUserData;
}
public IUserData UserData {
get {
if (HttpContext.Current?.User?.Identity?.IsAuthenticated) {
return registeredUserData;
}
return notLoggedInUserData;
}
}
}
public class ChartsController : ApiController {
private readonly IUserDataAccessor accessor;
public ChartsController(IUserDataAccessor accessor) {
this.accessor = accessor;
}
[HttpGet]
public IHttpActionResult SomeAction() {
var userData = accessor.UserData;
//...do something associated with user data
return OK();
}
}
这允许控制器保持精简,只依赖于抽象
public interface IUserDataAccessor {
IUserData UserData { get; }
}
public class UserDataAccessor : IUserDataAccessor {
private readonly RegisteredUserData registeredUserData;
private readonly NotLoggedInUserData notLoggedInUserData;
public UserDataAccessor(
RegisteredUserData registeredUserData,
NotLoggedInUserData notLoggedInUserData) {
this.registeredUserData = registeredUserData;
this.notLoggedInUserData = notLoggedInUserData;
}
public IUserData UserData {
get {
if (HttpContext.Current?.User?.Identity?.IsAuthenticated) {
return registeredUserData;
}
return notLoggedInUserData;
}
}
}
public class ChartsController : ApiController {
private readonly IUserDataAccessor accessor;
public ChartsController(IUserDataAccessor accessor) {
this.accessor = accessor;
}
[HttpGet]
public IHttpActionResult SomeAction() {
var userData = accessor.UserData;
//...do something associated with user data
return OK();
}
}
最后,确保抽象和它的实现已在组合根目录中的依赖项容器中注册。
//不工作
为我们提供了更多上下文。你说的不工作是什么意思?发生了什么?是否引发异常?还有什么吗?它在构造函数中总是未注册/未验证。您有更好的解决方案将经过身份验证的用户注入控制器构造函数吗?@TejasPatel我没有访问构造函数中的用户。正如你已经知道的那样,我在那个时候不在。它将仅在操作中可用。我在构造函数中注入访问器,它们通过操作本身获取用户。是-有没有办法在构造函数中注入经过身份验证的用户标识,因为我想创建存储库对象,并在构造函数中而不是在每个操作中将当前用户传递给它。否。你不理解。访问标识仅在操作的上下文中可用。不直接。因此,您可以在存储库中插入访问器,但您将从一个操作中调用存储库成员,该操作将在该操作中设置标识