Skip to content

Instantly share code, notes, and snippets.

View xtrmstep's full-sized avatar
🏠
Working from home

Alexander Goida xtrmstep

🏠
Working from home
  • Sofia, Bulgaria
View GitHub Profile
public class AuthHeadersInterceptor : Interceptor
{
public AuthHeadersInterceptor(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
var metadata = new Metadata
httpClientBuilder.ConfigureChannel(o =>
{
// add SSL credentials
o.Credentials = new SslCredentials();
// allow invalid/untrusted certificates
var httpClientHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
var httpClient = new HttpClient(httpClientHandler);
services.AddTransient<AuthHeadersInterceptor>();
services.AddHttpContextAccessor();
var httpClientBuilder = services.AddGrpcClient<MygRpcService.MygRpcServiceClient>(o => { o.Address = new Uri("grpc-endpoint-url"); });
httpClientBuilder.AddInterceptor<AuthHeadersInterceptor>();
httpClientBuilder.ConfigureChannel(o => o.Credentials = ChannelCredentials.Insecure);
public class JwtTokenValidator : ISecurityTokenValidator
{
public bool CanReadToken(string securityToken) => true;
public ClaimsPrincipal ValidateToken(string securityToken, TokenValidationParameters validationParameters, out SecurityToken validatedToken)
{
var handler = new JwtSecurityTokenHandler();
var tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
public void ConfigureServices(...) {
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o => {
var validator = new JwtTokenValidator(...);
o.SecurityTokenValidators.Add(validator);
});
services.AddAuthorization();
}
public void Configure(...) {
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(...
}
@xtrmstep
xtrmstep / memorySizeOfObject.js
Created August 18, 2019 07:26
calculate memory size of javascript object, it is not a accurate value!
function memorySizeOf(obj) {
var bytes = 0;
function sizeOf(obj) {
if(obj !== null && obj !== undefined) {
switch(typeof obj) {
case 'number':
bytes += 8;
break;
case 'string':
@xtrmstep
xtrmstep / Docker shell commands.sh
Created July 29, 2019 12:46 — forked from bahmutov/Docker shell commands.sh
A personal cheat sheet for running local Node project in a Docker container
# See list of docker virtual machines on the local box
$ docker-machine ls
NAME ACTIVE URL STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.9.1
# Note the host URL 192.168.99.100 - it will be used later!
# Build an image from current folder under given image name
$ docker build -t gleb/demo-app .
@xtrmstep
xtrmstep / browser_detect.js
Last active October 16, 2020 18:23
Detect browser using duck typing and User agent
function detectBrowserByFeatures() {
// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = /constructor/i.test(window.HTMLElement) || (function(p) {
return p.toString() === "[object SafariRemoteNotification]";