Controller(asp web api)中的HttpContext(User.Identity)无法正常工作

时间:2017-07-22 10:08:59

标签: c# authentication asp.net-web-api asp.net-identity

我在正确工作HttpContext.Current.User.Identity时遇到了一些问题。从Controller构造函数这不起作用,我必须实现这个方法。看看这个例子。

    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;
    }

}

1 个答案:

答案 0 :(得分:1)

首先,请求和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();
    }
}

最后确保抽象及其实现是在组合根中注册了依赖容器。