Friday, October 4, 2024

Logitech Mouse and Keyboard do not Work

I found a Logitech mouse and keyboard combo on clearance. The model is MK470 and it looks returned. For the steep discounted price, I decided to give a try. Expectedly, it didn't work, so that starts my troubleshooting. Battery is fine, no on/off button on keyboard, both mouse and keyboard are not working, no sign of damage, dongle is properly inserted into the USB port.

Short while later, I found that Logitech has a neat Connection Utility software. I downloaded it and ran it twice, once to reconnect the mouse and once for the keyboard.

My guess is the frequency and channel somehow was not lining up between the mouse and keyboard and the dongle. The previous buyer probably returned it because they were not working. But the connection is finally restored.

Sunday, September 29, 2024

AWS Cognito Error on Sign Up

I was exploring AWS Cognito for authentication. It works great, but I got the following error message after I tested the sign up process: An error was encountered with the requested page. I found out later that I misunderstood the AutoVerifiedAttributes field in my CloudFormation. I thought it would mark an email or phone number as verified without actually verifying them. Apparently, it means it will try to verify either email or phone number. So, when I set it to email, it sent a verification email and the sign up process went without error.

Friday, September 27, 2024

ASP.NET Application Crashed without Error Message

I encountered a strange error with ASP.NET Web API application. It runs fine locally, but when we deployed to Kubernetes cluster, it crashed as soon as it starts. And no error message was thrown.

So, I pulled the application to my local and it crashed as well no matter how I run it, dotnet cli, Docker Desktop, Visual Studio debug. The only one that runs fine is the version from the repo. At this point, there are only two possibilities, either the environment is the issue or the application is the issue, so I decided to deploy it to a different environment and it's still not working, so it must be something with the application.

Since it is the application, I tried to change the log level to Trace to get more information but no new error message that provides a hint on what's going on. Memory dump didn't work as the collector didn't have enough time to collect before the application crashed.

At the end, I decided to approach this the hard way. So, in my local, there are two versions. One is freshly pulled from the environment which is not working. The other one is from the source control repository which works. The most obvious difference between the two is the configuration, in this case, we use appsettings.json. But it looks fine.

My next guess is one of the dlls is probably the issue, so I started to try to break the one that works by substituting the file with different size one-by-one from the broken application. However, all the suspicious dlls don't seem to cause a problem.

As I run out of dlls, I start substituting the appsettings.json. It has different size, why not. And that's when the working version stops working. To zero in on the cause, I start removing fields in the appsettings.json file to see if I can make it to work. Finally, there's one field that's the cause. It looks similar to the following:

{
  "Settings": {
     "Key": "Value"
  }
}

Looking into the code that read that field, it is immediately obvious. In ASP.NET, there's a way to deserialize the field to an object and we can use enum for the value. Let say:


{
  "Settings": {
     "Pet": "Dog"
  }
}

The code to retrieve the setting will be similar to the following:

public enum Pets {Cat, Fish}

public class TheSettings {
  public Pets Pet { get; set; }
}

TheSettings settings = configuration.GetSection("Settings").Get<TheSettings>();

All looks great, except, the enum doesn't have the value specified in the appsettings.json file. In the example above, Dog is not an enum of Pets.  So, when it tries to deserialize the config, it breaks and somehow it didn't throw an error.

Fix either the enum or the value in the field in appsettings.json finally fixed the issue and the application can start without crashing.

Wednesday, September 25, 2024

JWT is not well formed in ASP.NET Web API JwtBearer .NET 8

 It never caused a problem for me to implement JwtBearer token validator, but this time it is really take my time to troubleshoot what's going on. Long story short, there's a breaking change going to .NET 8 and on top of that, the default package version doesn't solve the issue.

Here's how I implement my service:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => ...removed for brevity);
services.AddAuthorization();

...

app.UseAuthentication();
app.UseAuthorization();

But checking the bearer token, it was a completely valid token. I retrieved the token using a quick custom middleware.

app.Use(async (context, next) =>
{
    await next.Invoke();
    Debug.WriteLine(context.Request.Headers.Authorization);
});

app.UseAuthentication();

...

Then I validate the token in https://jwt.io.

The error that I received contains:

IDX14100: JWT is not well formed, there are no dots (.). The token needs to be in JWS or JWE Compact Serialization Format.

On top of that, the browser response header has the following header:

WWW-Authenticate: Bearer error="invalid_token"

Searching online, the most helpful hint is probably this thread: https://github.com/dotnet/aspnetcore/issues/52075

There are some suggestions in there, but the one that finally solves my problem is the fact that the following package version doesn't work:

Microsoft.IdentityModel.Protocols.OpenIdConnect 7.1.2

It was a transitive package and I have to upgrade it by installing the latest version, as of this time 8.1.0.

And that fixed my issue without any code change.



Friday, September 13, 2024

Swagger .NET 8 Error

Swashbuckle CLI was able to output schema of my API before, but this time, it throws this error message:

System.InvalidOperationException: A type named 'StartupProduction' or 'Startup' could not be found in assembly

I used top level statement with minimal API on .NET 8 and nothing is changed on that, so I was not able to find anything to do with Startup type.

After I investigate further by commenting line by line, I found out that the issue is on my switch statement.

So it looks like the following:

return config.Section?.Key switch
{
  Value1 => services.AddSingleton<Handler1>(),
  Value2 => services.AddSingleton<Handler2>(),
  _ => throw new InvalidOperationException();
}

Problem is the Section is pulled from appsettings.json and when the CLI runs, it doesn't have value, so it never returned the services object.

Changing the above to the following fixed the issue:

return config.Section == null ? services : config.Section.Key switch
{
  Value1 => services.AddSingleton<Handler1>(),
  Value2 => services.AddSingleton<Handler2>(),
  _ => throw new InvalidOperationException();
}


Thursday, September 12, 2024

Background Image on WordPress Editor

I realized that I don't have the Layout option under Styles menu in the Editor. I found out later that I have a bare minimum theme.json. And adding appearanceTools: true field cause it to show up. My theme.json became:

{
	"version": 3,
	"$schema": "https://schemas.wp.org/wp/6.6/theme.json",
	"settings": {
		"appearanceTools": true
	}	
}


WordPress Create Block Theme Plugin

 I'm working on a custom WordPress theme and I saw a very helpful plugin called "Create Block Theme" which is supposed to help developer create the theme.

So, for starting, I tried to edit one of my templates, but when I hit "Save Changes" under "Save Changes to Theme" section, I expected it to overwrite my template html file, but it didn't. I was checking permissions and potential bugs, but seems like everything is good.

After playing around a little, apparently, I need to hit "Save" first, so it records the customization in the database and then click the "Save Changes to Theme" will use the value from the database and modified the html file itself.