Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created December 28, 2019 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/3b8da5afda3307a52df412313698d21c to your computer and use it in GitHub Desktop.
Save xximjasonxx/3b8da5afda3307a52df412313698d21c to your computer and use it in GitHub Desktop.
public UserTokenBinding(IServiceProvider serviceProvider)
{
_readTokenService = serviceProvider.GetService<IReadTokenService>();
_configuration = serviceProvider.GetService<IConfiguration>();
}
public Task<IValueProvider> BindAsync(BindingContext context)
{
var headers = context.BindingData["Headers"] as IDictionary<string, string>;
var issuerToken = _configuration["JwtIssuer"];
if (!headers.ContainsKey("Authorization"))
return Task.FromResult<IValueProvider>(new UserTokenValueProvider(string.Empty, issuerToken, _readTokenService));
var userTokenGroups = Regex.Match(headers["Authorization"], @"^Bearer (\S+)$").Groups;
var userToken = userTokenGroups.ElementAt(1).Value;
return Task.FromResult<IValueProvider>(new UserTokenValueProvider(userToken, issuerToken, _readTokenService));
}
public ParameterDescriptor ToParameterDescriptor()
{
return new ParameterDescriptor();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment