How to access User in a service context in web API?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:55:11

You can create an intermediate service to provide that functionality

public interface IPrincipalProvider {
    IPrincipal User { get; }
}

public class WebApiPrincipalProvider : IPrincipalProvider {
    public IPrincipal User { 
        get {
            return HttpContext.Current != null
                ? HttpContext.Current.User 
                : null;
        }
    }
}

and inject it into the dependent service context

public class MyService : IService {
    private readonly IPrincipalProvider provider;

    public MyService(IPrincipalProvider provider) {
        this.provider = provider;
    }

    public MyModel MyServiceMethod() {
        var currentUser = provider.User;
        var name = currentUser.Identity.Name;

        //...use user....

        return model;
    }
}

Finally make sure abstraction and implementation are registered with DI container in composition root of main application so that when service is injected into controller it would also be able to access current request's user.

[Authorize]
public class MyController : ApiController {
    public readonly IService service;

    public MyController (IService service) {
        this.service = service;
    }

    [HttpGet]
    public IHttpActionResult MyGetActiom() {
        var model = service.MyServiceMethod();
        return Ok(model);
    }
}

When Identity framework authenticates a user the user principal is then set for the current context.

If hosted in IIS you can tap into HttpContext to access the user like in the example provided earlier. MVC and Web API basically do something similar to populate Controller.User and ApiController.User.

If self hosting there are other ways to access it.

That fact is that once authenticated, the user is available. Encapsulate it behind an abstraction and you can injected where ever it is needed outside of a controller.

Asp.net Core introduced something similar IHttpContextAccessor which allowed service classes to access the current HttpContext out side of controllers

public interface IHttpContextAccessor {
    HttpContext HttpContext { get; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!