将用户凭据从HttpRequestContext注入服务层

时间:2018-09-06 13:39:08

标签: c# asp.net .net asp.net-web-api2 ninject

我有一个可处理HTTP请求的.NET 4.6 WebApi 2.0 OWIN App。 这些请求已通过身份验证,我们将某些声明存储在RequestContext ClaimsIdentity(用户ID,客户端代码)中

我们还有我们的控制器,服务和存储库结构。

我们的服务是通过Ninject注入到Controller中的,而存储库是通过服务注入的。

当用户执行某些请求时,我们需要检查其权限。服务通常需要知道:UserId,ClientCode,用户有权访问的组以及每个组(读,写等)下的权限。最后一个仅存储在数据库中。

执行请求时的当前流程是:从RequestContext声明中获取userId和clientCode,使用该用户ID从数据库中获取组权限。创建具有这些属性的对象UserCredentials

该对象然后在被注入后 传递给服务实例 这是一个示例:

public PeopleController(IPeopleService peopleService, IUserManagementService userManagementService)
    {
        var clientCode = RequestContext.GetClientCode();
        var userId = RequestContext.GetUserId();
        var permissions = userManagementService.GetUserPermissions(userId, clientCode);
        }

        peopleService.InjectCredentials(new services.Models.UserCredentials()
        {
            ClientCode = clientCode,
            UserId = UserId,
            Permissions = UserPermissions

        });
        _peopleService = peopleService;
    }

对于我们来说,这看起来并不优雅,因为我们需要在各处重复该代码,并使IPeopleService依赖于用户,而用户不会忘记调用InjectCredentials方法。

所以我的问题是,如何使UserCredentials类可注入,以便可以将其作为参数传递给IPeopleService构造函数?

主要功能是它依赖于HttpRequestContext,后者仅在Controller本身上可用

谢谢

编辑:我知道UserCredentials对象也可以作为参数传递给每个服务方法(实际上比InjectCredentials方法要好)

2 个答案:

答案 0 :(得分:0)

不是注入模型, 在您的IUserManagementService实现中,注入IHttpContextAccessor。然后实现一种方法来执行您将在控制器中执行的操作。 让此方法负责为您获取用户详细信息。

2018-09-06T13:37:57.697321+00:00 app[api]: Release v1 created by user jonathn6@icloud.com
2018-09-06T13:37:57.785389+00:00 app[api]: Enable Logplex by user jonathn6@icloud.com
2018-09-06T13:37:57.785389+00:00 app[api]: Release v2 created by user jonathn6@icloud.com
2018-09-06T13:38:40.000000+00:00 app[api]: Build started by user jonathn6@icloud.com
2018-09-06T13:37:57.697321+00:00 app[api]: Initial release by user jonathn6@icloud.com
2018-09-06T13:39:05.206635+00:00 app[api]: Scaled to web@1:Free by user jonathn6@icloud.com
2018-09-06T13:39:05.188008+00:00 app[api]: Release v3 created by user jonathn6@icloud.com
2018-09-06T13:39:05.188008+00:00 app[api]: Deploy 257b0e8b by user jonathn6@icloud.com
2018-09-06T13:39:08.000000+00:00 app[api]: Build succeeded
2018-09-06T13:39:10.243037+00:00 heroku[web.1]: Starting process with command `npm start`
2018-09-06T13:39:12.459230+00:00 app[web.1]:
2018-09-06T13:39:12.459251+00:00 app[web.1]: > sample-app@1.0.0 start /app
2018-09-06T13:39:12.459255+00:00 app[web.1]:
2018-09-06T13:39:12.459253+00:00 app[web.1]: > webpack-dev-server --open
2018-09-06T13:39:13.990393+00:00 app[web.1]: Project is running at http://localhost:8080/
2018-09-06T13:39:13.990876+00:00 app[web.1]: webpack output is served from /
2018-09-06T13:39:13.990913+00:00 app[web.1]: Content not from webpack is served from /app/public
2018-09-06T13:39:13.990975+00:00 app[web.1]: 404s will fallback to /index.html
2018-09-06T13:39:13.999552+00:00 app[web.1]: internal/child_process.js:323
2018-09-06T13:39:13.999556+00:00 app[web.1]: throw errnoException(err, 'spawn');
2018-09-06T13:39:13.999557+00:00 app[web.1]: ^
2018-09-06T13:39:13.999559+00:00 app[web.1]:
2018-09-06T13:39:13.999560+00:00 app[web.1]: Error: spawn EACCES
2018-09-06T13:39:13.999562+00:00 app[web.1]: at _errnoException (util.js:992:11)
2018-09-06T13:39:13.999564+00:00 app[web.1]: at ChildProcess.spawn (internal/child_process.js:323:11)
2018-09-06T13:39:13.999565+00:00 app[web.1]: at Object.exports.spawn (child_process.js:502:9)
2018-09-06T13:39:13.999567+00:00 app[web.1]: at module.exports (/app/node_modules/opn/index.js:76:26)
2018-09-06T13:39:13.999569+00:00 app[web.1]: at reportReadiness (/app/node_modules/webpack-dev-server/bin/webpack-dev-server.js:474:5)
2018-09-06T13:39:13.999571+00:00 app[web.1]: at Server.server.listen (/app/node_modules/webpack-dev-server/bin/webpack-dev-server.js:439:7)
2018-09-06T13:39:13.999572+00:00 app[web.1]: at Server.returnValue.listeningApp.listen (/app/node_modules/webpack-dev-server/lib/Server.js:615:10)
2018-09-06T13:39:13.999575+00:00 app[web.1]: at emitNone (events.js:106:13)
2018-09-06T13:39:13.999573+00:00 app[web.1]: at Object.onceWrapper (events.js:313:30)
2018-09-06T13:39:13.999576+00:00 app[web.1]: at Server.emit (events.js:208:7)
2018-09-06T13:39:14.008212+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-09-06T13:39:14.008589+00:00 app[web.1]: npm ERR! errno 1
2018-09-06T13:39:14.009730+00:00 app[web.1]: npm ERR! sample-app@1.0.0 start: `webpack-dev-server --open`
2018-09-06T13:39:14.009893+00:00 app[web.1]: npm ERR! Exit status 1
2018-09-06T13:39:14.010124+00:00 app[web.1]: npm ERR!
2018-09-06T13:39:14.010297+00:00 app[web.1]: npm ERR! Failed at the sample-app@1.0.0 start script.
2018-09-06T13:39:14.010464+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-09-06T13:39:14.050770+00:00 app[web.1]:
2018-09-06T13:39:14.051004+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-09-06T13:39:14.051156+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2018-09-06T13_39_14_011Z-debug.log
2018-09-06T13:39:14.139768+00:00 heroku[web.1]: State changed from starting to crashed

最后,将该服务插入需要这些详细信息的任何其他服务中。

public class UserManagementService : IUserManagementService 
{
   private readonly IHttpContext httpContext;

   public UserManagementService (IHttpContext httpContext)
   {
      this.httpContext = httpContext;
   }



   public UserCredentials GetUserDetails()
   {

new services.Models.UserCredentials()
    {
        ClientCode = clientCode,
        UserId = UserId,
        Permissions = UserPermissions

    });
   }

}

答案 1 :(得分:0)

我发现了一种可能的方法,但是由于使用HttpContext.Current.user,我不建议这样做

kernel.Bind<UserCredentials>().ToMethod(CreateUserCredentials);


private static UserCredentials CreateUserCredentials(IContext arg)
    {
        var identity = HttpContext.Current.User as ClaimsPrincipal;

        int userId = identity.Identity.GetUserId();
        string clientCode = identity.Identity.GetClientCode();
        var userManagementService = kernel.Get<IUserManagementService>();
        if (userId != 0)
        {
            var permisssions = userManagementService.GetUserPermissions(userId, userType, clientCode);
        }
        return new UserCredentials()
        {
            ClientCode = clientCode,
            UserId = userId,
            Permissions = userManagementService.GetUserPermissions(userId, clientCode)
        };

    }

现在,UserCredentials对象可以作为参数添加到构造函数中,并且可以正确初始化。