private static ApiUserModel GetApiUserForRequest(IProvideUserContext context) { var principal = GetClaimsPrincipalFromContext(context); var userId = principal.Claims .Where(c => c.Type == ClaimTypes.NameIdentifier) .Select(c => c.Value).SingleOrDefault(); return(new ApiUserModel { Id = userId, }); }
public static HttpContext GetHttpContext(this IProvideUserContext provideContext) { if (provideContext == null) { throw new ArgumentNullException(nameof(provideContext)); } if (provideContext.UserContext is HttpContextUserContext httpUserContext) { return(httpUserContext.Context); } throw new ArgumentException($"can't resolve HttpContext from target UserContext"); }
private static ClaimsPrincipal GetClaimsPrincipalFromContext(IProvideUserContext context) { var userContext = context.UserContext; if (!userContext.TryGetValue("user", out var userObject)) { throw new InvalidOperationException("User value is missing in UserContext"); } if (userObject == null) { throw new InvalidOperationException("User value is null in UserContext"); } if (userObject is not ClaimsPrincipal principal) { throw new InvalidOperationException($"User object has incorrect type {userObject.GetType()}"); } return(principal); }
public static void SetUserContext(this IProvideUserContext context, IUserContext value) => SetValue(context.UserContext, value);
public static IUserContext GetUserContext(this IProvideUserContext context) => GetValue <IUserContext>(context.UserContext);
public static void SetDependencyInjector(this IProvideUserContext context, IDependencyInjector value) => SetValue(context.UserContext, value);
public static IDependencyInjector GetDependencyInjector(this IProvideUserContext context) => GetValue <IDependencyInjector>(context.UserContext);
private static IDictionary <string, object> GetCacheDictionary(this IProvideUserContext context) => context.UserContext;
public static T GetCache <T>(this IProvideUserContext context, string key) { if (!GetCacheDictionary(context).TryGetValue(key, out object cacheObj)) { return(default);