One may make an IPrincipal like so:
GenericIdentity id = new GenericIdentity("Tom Jaeschke");
string[] roles = { "Tinker", "Tailor", "Soldier", "Sailor" };
GenericPrincipal part = new GenericPrincipal(id, roles);
SetPrincipal(part);
SetPrincipal in the example above might do this:
private void SetPrincipal(IPrincipal principal)
{
Thread.CurrentPrincipal = principal;
if (HttpContext.Current != null)
{
HttpContext.Current.User = principal;
}
}
Taking stuff in and out of Thread.CurrentPrincipal might be a good way to manage who is logged in in a traditional web app where the UI has only one thread. In an ASP.NET Web API application however a MVC Controller and an ApiController are not going to have the same thread as the Web API methods may are returned asynchronously.
No comments:
Post a Comment