alirizaadiyahsi / Nucleus

Vue startup application template that uses ASP.NET Core API layered architecture at the back-end and JWT based authentication
MIT License
355 stars 86 forks source link

How can I fill CreateUserId property for new data in the Application layer #136

Closed eymel closed 4 years ago

eymel commented 4 years ago

Hello, I want to save a new object in the database. But I can't catch CreatedById in Application Layer. Can you help me?

alirizaadiyahsi commented 4 years ago

There is no implementation to get current user in AppService for now. You can get current user in API layer and pass it to AppService. Or, if you can implement this feature and send a PR, it is very welcome :)

alirizaadiyahsi commented 4 years ago

I added required parts to get current user Id in app service. You can inject IHttpContextAccessor and get user id or any data that you added to claims. For example:

public class RoleAppService : IRoleAppService
{
    ....
    private readonly IHttpContextAccessor _httpContextAccessor;

    public RoleAppService(...., IHttpContextAccessor httpContextAccessor)
    {
        ....
        _httpContextAccessor = httpContextAccessor;
    }

    public async Task<SomeResult> SomeMethod(SomeInput input)
    {
        var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

        return SomeResult;
    }