Asp.net web api WebApi外部承载身份验证和将用户注入职责

Asp.net web api WebApi外部承载身份验证和将用户注入职责,asp.net-web-api,dependency-injection,controller,authorization,Asp.net Web Api,Dependency Injection,Controller,Authorization,我有一个webapi项目和一个存储库项目 我已经配置使用oauth,它使用owin中间件承载令牌身份验证 我有一个具有多个存储库的工作单元 在存储库中,我希望根据登录的用户过滤数据 我希望所有存储库通过依赖项注入获得登录用户 我可以在webapi操作中访问登录的用户,但我很难确定是否/如何使用DI注入当前用户;因为授权是通过webapi授权进行的?: [Authorize(Roles = "User")] public IQueryable<Folder> Folders()

我有一个webapi项目和一个存储库项目

我已经配置使用oauth,它使用owin中间件承载令牌身份验证

我有一个具有多个存储库的工作单元

在存储库中,我希望根据登录的用户过滤数据

我希望所有存储库通过依赖项注入获得登录用户

我可以在webapi操作中访问登录的用户,但我很难确定是否/如何使用DI注入当前用户;因为授权是通过webapi授权进行的?:

[Authorize(Roles = "User")]
    public IQueryable<Folder> Folders()
    {
       // return UnitOfWork.FolderRepository.All().OrderBy(o=>o.FolderId).Skip(10).Take(50);
        var test = HttpContext.Current.GetOwinContext().Authentication;
        //test is populated with the logged on user here, but I don't want to set the user details of the UOW in every action in my controllers
        return UnitOfWork.FolderRepository.All();
    }
然后,在UserPrincipal的UnitOfWork setter中,我在包含的存储库中设置UserPrincipal:

public IPrincipal UserPrincipal
    {
        get
        {
            return this.userPrincipal;
        }
        set
        {
            this.userPrincipal = value;

            ((Repository<Folder>)FolderRepository).UserPrincipal = value;
        }
    }
公共IPrincipal用户主体
{
得到
{
返回此.userPrincipal;
}
设置
{
this.userPrincipal=值;
((存储库)FolderRepository).UserPrincipal=value;
}
}
现在可以了,但不能实现依赖注入


另外,我想知道这是一种“正确”的方法,还是一种更好的方法?

我在寻找同样的方法,并决定这样做。我认为这个答案也会与你相关。

我刚刚在服务类中添加了一个getter,它在请求时访问用户标识

public class MyService
{
    //ctor...

    public IEnumerable<Results> GetResults()
    {
        return _ResultRepository.GetResultsByUser(UserIdentity);
    }

    IIdentity UserIdentity
    {
        get { return Thread.CurrentPrincipal.Identity; }
    }
}
公共类MyService
{
//ctor。。。
公共IEnumerable GetResults()
{
return\u resultpository.GetResultsByUser(UserIdentity);
}
身份用户标识
{
获取{return Thread.CurrentPrincipal.Identity;}
}
}
using Project1.Web.Controllers;
using System;
using System.Linq;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace Project1.Web.Filters
{
public class InjectUserAttribute : ActionFilterAttribute
{

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var action = actionContext.ControllerContext.Controller;
            UowApiController ctrl = (UowApiController)actionContext.ControllerContext.Controller;
            ctrl.UnitOfWork.UserPrincipal = actionContext.RequestContext.Principal;
        }

}
public IPrincipal UserPrincipal
    {
        get
        {
            return this.userPrincipal;
        }
        set
        {
            this.userPrincipal = value;

            ((Repository<Folder>)FolderRepository).UserPrincipal = value;
        }
    }
public class MyService
{
    //ctor...

    public IEnumerable<Results> GetResults()
    {
        return _ResultRepository.GetResultsByUser(UserIdentity);
    }

    IIdentity UserIdentity
    {
        get { return Thread.CurrentPrincipal.Identity; }
    }
}