public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var tokenExists = context.HttpContext.Request.Headers.ContainsKey("Authorization"); if (tokenExists == false) { context.Result = new ContentResult { Content = JsonSerializer.Serialize(new { name = "error", description = "No bearer token was provided" }), ContentType = "application/json", StatusCode = 401 }; } var isValidHeader = AuthenticationHeaderValue.TryParse(context.HttpContext.Request.Headers["Authorization"], out AuthenticationHeaderValue accessToken); if (isValidHeader) { var userProfile = await SpotifyAuthorizationUtil.GetProfileFromTokenSpotify(accessToken.Parameter); if (userProfile != null) { context.HttpContext.Items["User"] = userProfile; await next(); return; } } context.Result = new ContentResult { Content = JsonSerializer.Serialize(new { name = "error", description = "Invalid authorization token" }), ContentType = "application/json", StatusCode = 401 }; }
public async ValueTask <object> InvokeMethodAsync( HubInvocationContext invocationContext, Func <HubInvocationContext, ValueTask <object> > next) { var accessTokenParam = invocationContext.HubMethodArguments.Where(pre => pre.ToString() == "accessToken").FirstOrDefault().ToString(); if (accessTokenParam == null) { throw new HubException("Auth token wasn't provided"); } var account = await SpotifyAuthorizationUtil.GetProfileFromTokenSpotify(accessTokenParam); if (account == null) { throw new HubException("Invalid auth token provided"); } invocationContext.HubMethodArguments.Append(account); return(await next(invocationContext)); }