protected async Task <RequestUser> GetRequestUserAsync(CancellationToken token) { var requestUser = User.ToIdentityUser(HttpContext.TraceIdentifier); var cookie = HttpContext.Request.ImpersonificationId(); if (cookie.HasValue) { var user = await _context.Users.SingleOrDefaultAsync(u => u.Id == cookie.Value, token); if (user != null) { var roles = user.Kind == ProfileKind.Consumer ? new List <string>() { _roleOptions.Consumer.Value } : new List <string>() { _roleOptions.Owner.Value, user.Kind == ProfileKind.Producer ? _roleOptions.Producer.Value : _roleOptions.Store.Value }; var uid = User.TryGetUserId(); Impersonification impersonification = null; if (uid.HasValue) { impersonification = new Impersonification(uid.Value, User.GetName()); } requestUser = new RequestUser(user.Id, user.Name, user.Email, roles, HttpContext.TraceIdentifier, impersonification); } } return(requestUser); }
public static RequestUser ToIdentityUser(this ClaimsPrincipal user, string requestId, Impersonification impersonification = null) { return(new RequestUser(user.TryGetUserId(), user.GetName(), user.GetEmail(), user.GetRoles(), requestId, impersonification)); }