Skip to content

Instantly share code, notes, and snippets.

View xiaomi7732's full-sized avatar

Saar Shen xiaomi7732

View GitHub Profile
@xiaomi7732
xiaomi7732 / ConfigureOptionsOfT.md
Last active September 24, 2022 19:02
What happens when you call ConfigureOptions<T> on IServiceCollection

What happens when you call ConfigureOptions<T> on IServiceCollection

It will findout all the interfaces on the target type, and if they are one of these interfaces:

  • IConfigureOptions<>
  • IPostConfigureOptions<>
  • IValidateOptions<>

It will register it by calling:

@xiaomi7732
xiaomi7732 / ConfigureJWTBearerOptions.md
Last active March 28, 2025 03:59
Configure JWT Bearer Options based on other options

Configure JWT Bearer Options based on another option

The problem

AddJwtBearer() provides a delegate to configure JwtBearerOptions, see example. The problem is that there's no way to set the value based on another option.

For example, if you created a custom AuthOptions, which includes basic information like the signing key, audience, issuer, and so on to be configured environment by environment, there's no way to access it in AddJwtBearer() method.

If you prefer to read the code, here is an implementation.

Solution with a caveat (See next section for a solution that works)

@xiaomi7732
xiaomi7732 / DetectDuplication.md
Created September 22, 2022 19:11
Detect duplications in a list in real projects

Detect duplications in real project scenario

Disclaimer: This is not leetcode implemenation, complexity is not considered. This is looking for an easy implemenation.

Problem

Assuming a list of string:

List<string> data = ...
@xiaomi7732
xiaomi7732 / .editorconfig
Last active December 5, 2024 22:34
My Minimum editorConfig file
# top-most EditorConfig file
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
@xiaomi7732
xiaomi7732 / KubectlPortForwarding.md
Created June 14, 2022 18:38
Hit K8s pod quickly for testing

Hit K8s pod quickly for testing

kubectl port-forward your-pod-name 8080:80

Then you can curl it locally:

curl http://localhost:8080/weatherforecast

JWT Multiple Roles Cheatsheet

Put multiple roles to the JWT token claims:

// Package: System.IdentityModel.Tokens.Jwt
JwtSecurityToken token = new JwtSecurityToken(
    issuer: "saar",
    audience: "saar-audience",
    claims: new[] {
@xiaomi7732
xiaomi7732 / WhyConfigureAwaitFalse.md
Created April 7, 2022 00:28
Why you should use ConfigureAwait(false) in your library code - explain by code

Enable GitHub Page for https://www.codewithsaar.net

Last time, I posted about binding a custom domain of https://stack.codewithsaar.net to GitHub Page, (Read it here), noticing that is actually a subdomain(stack.abc.com). Ever since, I have turned on another GitHub page for my own - https://www.codewithsaar.net (and also https://codewithsaar.net) on an apex domain.

Since DNS is not my day to day job, it took me a couple of try and error to get it work. So, I am here to do a follow up.

Here's the final goal:

@xiaomi7732
xiaomi7732 / PublishFullHTTPSWebSitePages.2022.md
Last active March 3, 2022 01:47
I published a full HTTPS encrypted website on GitHub pages (in year 2022)

I published a full HTTPS encrypted website on GitHub pages (in year 2022)

It had always been a pain to host a small/basic/quick/light-weight website on a custom domain that supports HTTPS. Common issues are Domain verification, certificate rotation/management, and quick iteration with a easy to understand CI/CD pipeline.

However, this year, when I try it again with GitHub pages, it blowed my mind. I think it addresses all those pain points and I have to say, good job GitHub!

image

(Image comes from internet)

I'll put down some reasons to enable HTTPs on a custom domain of my own. If you only want to see how I did it, jump to How I did it.

Troubleshooting guidance for DI

This is supposed to be a quick checklist / cheatsheet to help debug DI failures.

Common errors checklist

These are easy issues that happened to me.

Having a quick check, sometimes, this is the most efficent way to catch a bug: